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.

TestXSLFSimpleShape.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 junit.framework.TestCase;
  17. import org.apache.poi.util.Units;
  18. import org.apache.poi.xslf.usermodel.LineCap;
  19. import org.apache.poi.xslf.usermodel.LineDash;
  20. import org.apache.poi.xslf.XSLFTestDataSamples;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.STLineCap;
  22. import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
  24. import java.awt.*;
  25. /**
  26. * @author Yegor Kozlov
  27. */
  28. public class TestXSLFSimpleShape extends TestCase {
  29. public void testLineStyles() {
  30. XMLSlideShow ppt = new XMLSlideShow();
  31. XSLFSlide slide = ppt.createSlide();
  32. XSLFSimpleShape shape = slide.createAutoShape();
  33. assertEquals(1, slide.getShapes().length);
  34. // line properties are not set by default
  35. assertFalse(shape.getSpPr().isSetLn());
  36. assertEquals(0., shape.getLineWidth());
  37. assertEquals(null, shape.getLineColor());
  38. assertEquals(null, shape.getLineDash());
  39. assertEquals(null, shape.getLineCap());
  40. shape.setLineWidth(0);
  41. shape.setLineColor(null);
  42. shape.setLineDash(null);
  43. shape.setLineCap(null);
  44. // still no line properties
  45. assertFalse(shape.getSpPr().isSetLn());
  46. // line width
  47. shape.setLineWidth(1.0);
  48. assertEquals(1.0, shape.getLineWidth());
  49. assertEquals(Units.EMU_PER_POINT, shape.getSpPr().getLn().getW());
  50. shape.setLineWidth(5.5);
  51. assertEquals(5.5, shape.getLineWidth());
  52. assertEquals(Units.toEMU(5.5), shape.getSpPr().getLn().getW());
  53. shape.setLineWidth(0.0);
  54. // setting line width to zero unsets the W attribute
  55. assertFalse(shape.getSpPr().getLn().isSetW());
  56. // line cap
  57. shape.setLineCap(LineCap.FLAT);
  58. assertEquals(LineCap.FLAT, shape.getLineCap());
  59. assertEquals(STLineCap.FLAT, shape.getSpPr().getLn().getCap());
  60. shape.setLineCap(LineCap.SQUARE);
  61. assertEquals(LineCap.SQUARE, shape.getLineCap());
  62. assertEquals(STLineCap.SQ, shape.getSpPr().getLn().getCap());
  63. shape.setLineCap(LineCap.ROUND);
  64. assertEquals(LineCap.ROUND, shape.getLineCap());
  65. assertEquals(STLineCap.RND, shape.getSpPr().getLn().getCap());
  66. shape.setLineCap(null);
  67. // setting cap to null unsets the Cap attribute
  68. assertFalse(shape.getSpPr().getLn().isSetCap());
  69. // line dash
  70. shape.setLineDash(LineDash.SOLID);
  71. assertEquals(LineDash.SOLID, shape.getLineDash());
  72. assertEquals(STPresetLineDashVal.SOLID, shape.getSpPr().getLn().getPrstDash().getVal());
  73. shape.setLineDash(LineDash.DASH_DOT);
  74. assertEquals(LineDash.DASH_DOT, shape.getLineDash());
  75. assertEquals(STPresetLineDashVal.DASH_DOT, shape.getSpPr().getLn().getPrstDash().getVal());
  76. shape.setLineDash(LineDash.LG_DASH_DOT);
  77. assertEquals(LineDash.LG_DASH_DOT, shape.getLineDash());
  78. assertEquals(STPresetLineDashVal.LG_DASH_DOT, shape.getSpPr().getLn().getPrstDash().getVal());
  79. shape.setLineDash(null);
  80. // setting dash width to null unsets the Dash element
  81. assertFalse(shape.getSpPr().getLn().isSetPrstDash());
  82. // line color
  83. assertFalse(shape.getSpPr().getLn().isSetSolidFill());
  84. shape.setLineColor(Color.RED);
  85. assertEquals(Color.RED, shape.getLineColor());
  86. assertTrue(shape.getSpPr().getLn().isSetSolidFill());
  87. shape.setLineColor(Color.BLUE);
  88. assertEquals(Color.BLUE, shape.getLineColor());
  89. assertTrue(shape.getSpPr().getLn().isSetSolidFill());
  90. shape.setLineColor(null);
  91. assertEquals(null, shape.getLineColor());
  92. // setting dash width to null unsets the SolidFill element
  93. assertFalse(shape.getSpPr().getLn().isSetSolidFill());
  94. }
  95. public void testFill() {
  96. XMLSlideShow ppt = new XMLSlideShow();
  97. XSLFSlide slide = ppt.createSlide();
  98. XSLFAutoShape shape = slide.createAutoShape();
  99. // line properties are not set by default
  100. assertFalse(shape.getSpPr().isSetSolidFill());
  101. assertNull(shape.getFillColor());
  102. shape.setFillColor(null);
  103. assertNull(shape.getFillColor());
  104. assertFalse(shape.getSpPr().isSetSolidFill());
  105. shape.setFillColor(Color.RED);
  106. assertEquals(Color.RED, shape.getFillColor());
  107. shape.setFillColor(Color.DARK_GRAY);
  108. assertEquals(Color.DARK_GRAY, shape.getFillColor());
  109. assertTrue(shape.getSpPr().isSetSolidFill());
  110. shape.setFillColor(null);
  111. assertNull(shape.getFillColor());
  112. assertFalse(shape.getSpPr().isSetSolidFill());
  113. }
  114. public void testDefaultProperties() {
  115. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
  116. XSLFSlide slide6 = ppt.getSlides()[5];
  117. XSLFShape[] shapes = slide6.getShapes();
  118. for(int i = 1; i < shapes.length; i++){
  119. XSLFSimpleShape s = (XSLFSimpleShape) shapes[i];
  120. // all shapes have a theme color="accent1"
  121. assertEquals("accent1", s.getSpStyle().getFillRef().getSchemeClr().getVal().toString());
  122. assertEquals(2.0, s.getLineWidth());
  123. assertEquals(LineCap.FLAT, s.getLineCap());
  124. // YK: calculated color is slightly different from PowerPoint
  125. assertEquals(new Color(40, 65, 95), s.getLineColor());
  126. }
  127. XSLFSimpleShape s0 = (XSLFSimpleShape) shapes[0];
  128. // fill is not set
  129. assertNull(s0.getSpPr().getSolidFill());
  130. assertEquals(slide6.getTheme().getColor("accent1").getColor(), s0.getFillColor());
  131. assertEquals(new Color(79, 129, 189), s0.getFillColor());
  132. // lighter 80%
  133. XSLFSimpleShape s1 = (XSLFSimpleShape)shapes[1];
  134. CTSchemeColor ref1 = s1.getSpPr().getSolidFill().getSchemeClr();
  135. assertEquals(1, ref1.sizeOfLumModArray());
  136. assertEquals(1, ref1.sizeOfLumOffArray());
  137. assertEquals(20000, ref1.getLumModArray(0).getVal());
  138. assertEquals(80000, ref1.getLumOffArray(0).getVal());
  139. assertEquals("accent1", ref1.getVal().toString());
  140. assertEquals(new Color(220, 230, 242), s1.getFillColor());
  141. // lighter 60%
  142. XSLFSimpleShape s2 = (XSLFSimpleShape)shapes[2];
  143. CTSchemeColor ref2 = s2.getSpPr().getSolidFill().getSchemeClr();
  144. assertEquals(1, ref2.sizeOfLumModArray());
  145. assertEquals(1, ref2.sizeOfLumOffArray());
  146. assertEquals(40000, ref2.getLumModArray(0).getVal());
  147. assertEquals(60000, ref2.getLumOffArray(0).getVal());
  148. assertEquals("accent1", ref2.getVal().toString());
  149. assertEquals(new Color(185, 205, 229), s2.getFillColor());
  150. // lighter 40%
  151. XSLFSimpleShape s3 = (XSLFSimpleShape)shapes[3];
  152. CTSchemeColor ref3 = s3.getSpPr().getSolidFill().getSchemeClr();
  153. assertEquals(1, ref3.sizeOfLumModArray());
  154. assertEquals(1, ref3.sizeOfLumOffArray());
  155. assertEquals(60000, ref3.getLumModArray(0).getVal());
  156. assertEquals(40000, ref3.getLumOffArray(0).getVal());
  157. assertEquals("accent1", ref3.getVal().toString());
  158. assertEquals(new Color(149, 179, 215), s3.getFillColor());
  159. // darker 25%
  160. XSLFSimpleShape s4 = (XSLFSimpleShape)shapes[4];
  161. CTSchemeColor ref4 = s4.getSpPr().getSolidFill().getSchemeClr();
  162. assertEquals(1, ref4.sizeOfLumModArray());
  163. assertEquals(0, ref4.sizeOfLumOffArray());
  164. assertEquals(75000, ref4.getLumModArray(0).getVal());
  165. assertEquals("accent1", ref3.getVal().toString());
  166. // YK: calculated color is slightly different from PowerPoint
  167. assertEquals(new Color(59, 97, 142), s4.getFillColor());
  168. XSLFSimpleShape s5 = (XSLFSimpleShape)shapes[5];
  169. CTSchemeColor ref5 = s5.getSpPr().getSolidFill().getSchemeClr();
  170. assertEquals(1, ref5.sizeOfLumModArray());
  171. assertEquals(0, ref5.sizeOfLumOffArray());
  172. assertEquals(50000, ref5.getLumModArray(0).getVal());
  173. assertEquals("accent1", ref5.getVal().toString());
  174. // YK: calculated color is slightly different from PowerPoint
  175. assertEquals(new Color(40, 65, 95), s5.getFillColor());
  176. }
  177. public void testAnchor(){
  178. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
  179. XSLFSlide[] slide = ppt.getSlides();
  180. XSLFSlide slide2 = slide[1];
  181. XSLFSlideLayout layout2 = slide2.getSlideLayout();
  182. XSLFShape[] shapes2 = slide2.getShapes();
  183. XSLFTextShape sh1 = (XSLFTextShape)shapes2[0];
  184. assertEquals(Placeholder.CENTERED_TITLE, sh1.getTextType());
  185. assertEquals("PPTX Title", sh1.getText());
  186. assertNull(sh1.getSpPr().getXfrm()); // xfrm is not set, the query is delegated to the slide layout
  187. assertEquals(sh1.getAnchor(), layout2.getTextShapeByType(Placeholder.CENTERED_TITLE).getAnchor());
  188. XSLFTextShape sh2 = (XSLFTextShape)shapes2[1];
  189. assertEquals("Subtitle\nAnd second line", sh2.getText());
  190. assertEquals(Placeholder.SUBTITLE, sh2.getTextType());
  191. assertNull(sh2.getSpPr().getXfrm()); // xfrm is not set, the query is delegated to the slide layout
  192. assertEquals(sh2.getAnchor(), layout2.getTextShapeByType(Placeholder.SUBTITLE).getAnchor());
  193. XSLFSlide slide5 = slide[4];
  194. XSLFSlideLayout layout5 = slide5.getSlideLayout();
  195. XSLFTextShape shTitle = slide5.getTextShapeByType(Placeholder.TITLE);
  196. assertEquals("Hyperlinks", shTitle.getText());
  197. // xfrm is not set, the query is delegated to the slide layout
  198. assertNull(shTitle.getSpPr().getXfrm());
  199. // xfrm is not set, the query is delegated to the slide master
  200. assertNull(layout5.getTextShapeByType(Placeholder.TITLE).getSpPr().getXfrm());
  201. assertNotNull(layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE).getSpPr().getXfrm());
  202. assertEquals(shTitle.getAnchor(), layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE).getAnchor());
  203. }
  204. }