Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TestXSLFSimpleShape.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import static org.junit.Assert.fail;
  22. import java.awt.*;
  23. import java.awt.image.BufferedImage;
  24. import java.io.IOException;
  25. import java.util.List;
  26. import org.apache.poi.sl.draw.DrawFactory;
  27. import org.apache.poi.sl.draw.geom.TestPresetGeometries;
  28. import org.apache.poi.sl.usermodel.Placeholder;
  29. import org.apache.poi.sl.usermodel.Slide;
  30. import org.apache.poi.sl.usermodel.StrokeStyle.LineCap;
  31. import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
  32. import org.apache.poi.util.Units;
  33. import org.apache.poi.xslf.XSLFTestDataSamples;
  34. import org.junit.Test;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTEffectStyleItem;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTEffectStyleList;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
  40. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  41. import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrix;
  42. import org.openxmlformats.schemas.drawingml.x2006.main.STLineCap;
  43. import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
  44. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  45. /**
  46. * @author Yegor Kozlov
  47. */
  48. public class TestXSLFSimpleShape {
  49. @Test
  50. public void testLineStyles() throws IOException {
  51. XMLSlideShow ppt = new XMLSlideShow();
  52. XSLFSlide slide = ppt.createSlide();
  53. XSLFSimpleShape shape = slide.createAutoShape();
  54. assertEquals(1, slide.getShapes().size());
  55. // line properties are not set by default
  56. assertFalse(shape.getSpPr().isSetLn());
  57. assertEquals(0., shape.getLineWidth(), 0);
  58. assertEquals(null, shape.getLineColor());
  59. assertEquals(null, shape.getLineDash());
  60. assertEquals(null, shape.getLineCap());
  61. shape.setLineWidth(0);
  62. shape.setLineColor(null);
  63. shape.setLineDash(null);
  64. shape.setLineCap(null);
  65. // still no line properties
  66. assertFalse(shape.getSpPr().isSetLn());
  67. // line width
  68. shape.setLineWidth(1.0);
  69. assertEquals(1.0, shape.getLineWidth(), 0);
  70. assertEquals(Units.EMU_PER_POINT, shape.getSpPr().getLn().getW());
  71. shape.setLineWidth(5.5);
  72. assertEquals(5.5, shape.getLineWidth(), 0);
  73. assertEquals(Units.toEMU(5.5), shape.getSpPr().getLn().getW());
  74. shape.setLineWidth(0.0);
  75. // setting line width to zero unsets the W attribute
  76. assertFalse(shape.getSpPr().getLn().isSetW());
  77. // line cap
  78. shape.setLineCap(LineCap.FLAT);
  79. assertEquals(LineCap.FLAT, shape.getLineCap());
  80. assertEquals(STLineCap.FLAT, shape.getSpPr().getLn().getCap());
  81. shape.setLineCap(LineCap.SQUARE);
  82. assertEquals(LineCap.SQUARE, shape.getLineCap());
  83. assertEquals(STLineCap.SQ, shape.getSpPr().getLn().getCap());
  84. shape.setLineCap(LineCap.ROUND);
  85. assertEquals(LineCap.ROUND, shape.getLineCap());
  86. assertEquals(STLineCap.RND, shape.getSpPr().getLn().getCap());
  87. shape.setLineCap(null);
  88. // setting cap to null unsets the Cap attribute
  89. assertFalse(shape.getSpPr().getLn().isSetCap());
  90. // line dash
  91. shape.setLineDash(LineDash.SOLID);
  92. assertEquals(LineDash.SOLID, shape.getLineDash());
  93. assertEquals(STPresetLineDashVal.SOLID, shape.getSpPr().getLn().getPrstDash().getVal());
  94. shape.setLineDash(LineDash.DASH_DOT);
  95. assertEquals(LineDash.DASH_DOT, shape.getLineDash());
  96. assertEquals(STPresetLineDashVal.DASH_DOT, shape.getSpPr().getLn().getPrstDash().getVal());
  97. shape.setLineDash(LineDash.LG_DASH_DOT);
  98. assertEquals(LineDash.LG_DASH_DOT, shape.getLineDash());
  99. assertEquals(STPresetLineDashVal.LG_DASH_DOT, shape.getSpPr().getLn().getPrstDash().getVal());
  100. shape.setLineDash(null);
  101. // setting dash width to null unsets the Dash element
  102. assertFalse(shape.getSpPr().getLn().isSetPrstDash());
  103. // line color
  104. assertFalse(shape.getSpPr().getLn().isSetSolidFill());
  105. shape.setLineColor(Color.RED);
  106. assertEquals(Color.RED, shape.getLineColor());
  107. assertTrue(shape.getSpPr().getLn().isSetSolidFill());
  108. shape.setLineColor(Color.BLUE);
  109. assertEquals(Color.BLUE, shape.getLineColor());
  110. assertTrue(shape.getSpPr().getLn().isSetSolidFill());
  111. shape.setLineColor(null);
  112. assertEquals(null, shape.getLineColor());
  113. // setting dash width to null unsets the SolidFill element
  114. assertFalse(shape.getSpPr().getLn().isSetSolidFill());
  115. XSLFSimpleShape ln2 = slide.createAutoShape();
  116. ln2.setLineDash(LineDash.DOT);
  117. assertEquals(LineDash.DOT, ln2.getLineDash());
  118. ln2.setLineWidth(0.);
  119. assertEquals(0., ln2.getLineWidth(), 0);
  120. XSLFSimpleShape ln3 = slide.createAutoShape();
  121. ln3.setLineWidth(1.);
  122. assertEquals(1., ln3.getLineWidth(), 0);
  123. ln3.setLineDash(null);
  124. assertEquals(null, ln3.getLineDash());
  125. ln3.setLineCap(null);
  126. assertEquals(null, ln3.getLineDash());
  127. ppt.close();
  128. }
  129. @Test
  130. public void testFill() throws IOException {
  131. XMLSlideShow ppt = new XMLSlideShow();
  132. XSLFSlide slide = ppt.createSlide();
  133. XSLFAutoShape shape = slide.createAutoShape();
  134. // line properties are not set by default
  135. assertFalse(shape.getSpPr().isSetSolidFill());
  136. assertNull(shape.getFillColor());
  137. shape.setFillColor(null);
  138. assertNull(shape.getFillColor());
  139. assertFalse(shape.getSpPr().isSetSolidFill());
  140. shape.setFillColor(Color.RED);
  141. assertEquals(Color.RED, shape.getFillColor());
  142. shape.setFillColor(Color.DARK_GRAY);
  143. assertEquals(Color.DARK_GRAY, shape.getFillColor());
  144. assertTrue(shape.getSpPr().isSetSolidFill());
  145. shape.setFillColor(null);
  146. assertNull(shape.getFillColor());
  147. assertFalse(shape.getSpPr().isSetSolidFill());
  148. ppt.close();
  149. }
  150. @Test
  151. public void testDefaultProperties() throws IOException {
  152. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
  153. XSLFSlide slide6 = ppt.getSlides().get(5);
  154. List<XSLFShape> shapes = slide6.getShapes();
  155. for(XSLFShape xs : shapes){
  156. XSLFSimpleShape s = (XSLFSimpleShape)xs;
  157. // all shapes have a theme color="accent1"
  158. assertEquals("accent1", s.getSpStyle().getFillRef().getSchemeClr().getVal().toString());
  159. assertEquals(2.0, s.getLineWidth(), 0);
  160. assertEquals(LineCap.FLAT, s.getLineCap());
  161. assertEquals(new Color(79,129,189), s.getLineColor());
  162. }
  163. XSLFSimpleShape s0 = (XSLFSimpleShape) shapes.get(0);
  164. // fill is not set
  165. assertNull(s0.getSpPr().getSolidFill());
  166. //assertEquals(slide6.getTheme().getColor("accent1").getColor(), s0.getFillColor());
  167. assertEquals(new Color(79, 129, 189), s0.getFillColor());
  168. // lighter 80%
  169. XSLFSimpleShape s1 = (XSLFSimpleShape)shapes.get(1);
  170. CTSchemeColor ref1 = s1.getSpPr().getSolidFill().getSchemeClr();
  171. assertEquals(1, ref1.sizeOfLumModArray());
  172. assertEquals(1, ref1.sizeOfLumOffArray());
  173. assertEquals(20000, ref1.getLumModArray(0).getVal());
  174. assertEquals(80000, ref1.getLumOffArray(0).getVal());
  175. assertEquals("accent1", ref1.getVal().toString());
  176. assertEquals(new Color(79, 129, 189), s1.getFillColor());
  177. // lighter 60%
  178. XSLFSimpleShape s2 = (XSLFSimpleShape)shapes.get(2);
  179. CTSchemeColor ref2 = s2.getSpPr().getSolidFill().getSchemeClr();
  180. assertEquals(1, ref2.sizeOfLumModArray());
  181. assertEquals(1, ref2.sizeOfLumOffArray());
  182. assertEquals(40000, ref2.getLumModArray(0).getVal());
  183. assertEquals(60000, ref2.getLumOffArray(0).getVal());
  184. assertEquals("accent1", ref2.getVal().toString());
  185. assertEquals(new Color(79, 129, 189), s2.getFillColor());
  186. // lighter 40%
  187. XSLFSimpleShape s3 = (XSLFSimpleShape)shapes.get(3);
  188. CTSchemeColor ref3 = s3.getSpPr().getSolidFill().getSchemeClr();
  189. assertEquals(1, ref3.sizeOfLumModArray());
  190. assertEquals(1, ref3.sizeOfLumOffArray());
  191. assertEquals(60000, ref3.getLumModArray(0).getVal());
  192. assertEquals(40000, ref3.getLumOffArray(0).getVal());
  193. assertEquals("accent1", ref3.getVal().toString());
  194. assertEquals(new Color(79, 129, 189), s3.getFillColor());
  195. // darker 25%
  196. XSLFSimpleShape s4 = (XSLFSimpleShape)shapes.get(4);
  197. CTSchemeColor ref4 = s4.getSpPr().getSolidFill().getSchemeClr();
  198. assertEquals(1, ref4.sizeOfLumModArray());
  199. assertEquals(0, ref4.sizeOfLumOffArray());
  200. assertEquals(75000, ref4.getLumModArray(0).getVal());
  201. assertEquals("accent1", ref3.getVal().toString());
  202. assertEquals(new Color(79, 129, 189), s4.getFillColor());
  203. XSLFSimpleShape s5 = (XSLFSimpleShape)shapes.get(5);
  204. CTSchemeColor ref5 = s5.getSpPr().getSolidFill().getSchemeClr();
  205. assertEquals(1, ref5.sizeOfLumModArray());
  206. assertEquals(0, ref5.sizeOfLumOffArray());
  207. assertEquals(50000, ref5.getLumModArray(0).getVal());
  208. assertEquals("accent1", ref5.getVal().toString());
  209. assertEquals(new Color(79, 129, 189), s5.getFillColor());
  210. ppt.close();
  211. }
  212. @Test
  213. public void testAnchor() throws IOException {
  214. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
  215. List<XSLFSlide> slide = ppt.getSlides();
  216. XSLFSlide slide2 = slide.get(1);
  217. XSLFSlideLayout layout2 = slide2.getSlideLayout();
  218. List<XSLFShape> shapes2 = slide2.getShapes();
  219. XSLFTextShape sh1 = (XSLFTextShape)shapes2.get(0);
  220. assertEquals(Placeholder.CENTERED_TITLE, sh1.getTextType());
  221. assertEquals("PPTX Title", sh1.getText());
  222. assertNull(sh1.getSpPr().getXfrm()); // xfrm is not set, the query is delegated to the slide layout
  223. assertEquals(sh1.getAnchor(), layout2.getTextShapeByType(Placeholder.CENTERED_TITLE).getAnchor());
  224. XSLFTextShape sh2 = (XSLFTextShape)shapes2.get(1);
  225. assertEquals("Subtitle\nAnd second line", sh2.getText());
  226. assertEquals(Placeholder.SUBTITLE, sh2.getTextType());
  227. assertNull(sh2.getSpPr().getXfrm()); // xfrm is not set, the query is delegated to the slide layout
  228. assertEquals(sh2.getAnchor(), layout2.getTextShapeByType(Placeholder.SUBTITLE).getAnchor());
  229. XSLFSlide slide5 = slide.get(4);
  230. XSLFSlideLayout layout5 = slide5.getSlideLayout();
  231. XSLFTextShape shTitle = slide5.getTextShapeByType(Placeholder.TITLE);
  232. assertEquals("Hyperlinks", shTitle.getText());
  233. // xfrm is not set, the query is delegated to the slide layout
  234. assertNull(shTitle.getSpPr().getXfrm());
  235. // xfrm is not set, the query is delegated to the slide master
  236. assertNull(layout5.getTextShapeByType(Placeholder.TITLE).getSpPr().getXfrm());
  237. assertNotNull(layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE).getSpPr().getXfrm());
  238. assertEquals(shTitle.getAnchor(), layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE).getAnchor());
  239. ppt.close();
  240. }
  241. @SuppressWarnings({ "deprecation", "unused" })
  242. @Test
  243. public void testShadowEffects() throws IOException{
  244. XMLSlideShow ppt = new XMLSlideShow();
  245. XSLFSlide slide = ppt.createSlide();
  246. CTStyleMatrix styleMatrix = slide.getTheme().getXmlObject().getThemeElements().getFmtScheme();
  247. CTEffectStyleList lst = styleMatrix.getEffectStyleLst();
  248. assertNotNull(lst);
  249. for(CTEffectStyleItem ef : lst.getEffectStyleArray()){
  250. CTOuterShadowEffect obj = ef.getEffectLst().getOuterShdw();
  251. }
  252. ppt.close();
  253. }
  254. @Test
  255. public void testValidGeometry() throws Exception {
  256. XMLSlideShow ppt = new XMLSlideShow();
  257. XSLFSlide slide = ppt.createSlide();
  258. XSLFSimpleShape shape = slide.createAutoShape();
  259. CTShapeProperties spPr = shape.getSpPr();
  260. CTPresetGeometry2D prstGeom = CTPresetGeometry2D.Factory.newInstance();
  261. prstGeom.setPrst(STShapeType.Enum.forInt(1));
  262. assertNotNull(prstGeom.getPrst());
  263. assertNotNull(prstGeom.getPrst().toString());
  264. assertNotNull(spPr.getPrstGeom());
  265. spPr.setPrstGeom(prstGeom);
  266. assertNotNull(spPr.getPrstGeom().getPrst());
  267. assertNotNull(spPr.getPrstGeom().getPrst().toString());
  268. assertNotNull(shape.getGeometry());
  269. ppt.close();
  270. }
  271. @Test
  272. public void testInvalidGeometry() throws Exception {
  273. XMLSlideShow ppt = new XMLSlideShow();
  274. XSLFSlide slide = ppt.createSlide();
  275. XSLFSimpleShape shape = slide.createAutoShape();
  276. CTShapeProperties spPr = shape.getSpPr();
  277. CTPresetGeometry2D prstGeom = CTPresetGeometry2D.Factory.newInstance();
  278. prstGeom.setPrst(STShapeType.Enum.forInt(1));
  279. assertNotNull(prstGeom.getPrst());
  280. assertNotNull(prstGeom.getPrst().toString());
  281. assertNotNull(spPr.getPrstGeom());
  282. spPr.setPrstGeom(prstGeom);
  283. assertNotNull(spPr.getPrstGeom().getPrst());
  284. assertNotNull(spPr.getPrstGeom().getPrst().toString());
  285. try {
  286. // cause the geometries to be not found
  287. TestPresetGeometries.clearPreset();
  288. try {
  289. shape.getGeometry();
  290. fail("Should fail without the geometry");
  291. } catch (IllegalStateException e) {
  292. assertTrue(e.getMessage(), e.getMessage().contains("line"));
  293. }
  294. } finally {
  295. // reset to not affect other tests
  296. TestPresetGeometries.resetPreset();
  297. }
  298. ppt.close();
  299. }
  300. @SuppressWarnings("Duplicates")
  301. @Test
  302. public void testArrayStoreException() throws IOException {
  303. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("aascu.org_workarea_downloadasset.aspx_id=5864.pptx");
  304. Dimension pgsize = ppt.getPageSize();
  305. for (Slide<?,?> s : ppt.getSlides()) {
  306. //System.out.println("Slide: " + s);
  307. BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_ARGB);
  308. Graphics2D graphics = img.createGraphics();
  309. DrawFactory.getInstance(graphics).fixFonts(graphics);
  310. // default rendering options
  311. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  312. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  313. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  314. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  315. // draw stuff
  316. s.draw(graphics);
  317. graphics.dispose();
  318. img.flush();
  319. }
  320. ppt.close();
  321. }
  322. }