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.

XSLFConnectorShape.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import org.apache.poi.util.Beta;
  21. import org.apache.poi.xslf.model.geom.Outline;
  22. import org.apache.poi.xslf.model.geom.Path;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnectorNonVisual;
  34. import java.awt.Shape;
  35. import java.awt.geom.AffineTransform;
  36. import java.awt.geom.Ellipse2D;
  37. import java.awt.geom.GeneralPath;
  38. import java.awt.geom.Rectangle2D;
  39. import java.util.ArrayList;
  40. import java.util.Collections;
  41. import java.util.List;
  42. /**
  43. * Specifies a connection shape.
  44. *
  45. * @author Yegor Kozlov
  46. */
  47. @Beta
  48. public class XSLFConnectorShape extends XSLFSimpleShape {
  49. /*package*/ XSLFConnectorShape(CTConnector shape, XSLFSheet sheet) {
  50. super(shape, sheet);
  51. }
  52. /**
  53. * @param shapeId 1-based shapeId
  54. */
  55. static CTConnector prototype(int shapeId) {
  56. CTConnector ct = CTConnector.Factory.newInstance();
  57. CTConnectorNonVisual nvSpPr = ct.addNewNvCxnSpPr();
  58. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  59. cnv.setName("Connector " + shapeId);
  60. cnv.setId(shapeId + 1);
  61. nvSpPr.addNewCNvCxnSpPr();
  62. nvSpPr.addNewNvPr();
  63. CTShapeProperties spPr = ct.addNewSpPr();
  64. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  65. prst.setPrst(STShapeType.LINE);
  66. prst.addNewAvLst();
  67. CTLineProperties ln = spPr.addNewLn();
  68. return ct;
  69. }
  70. /**
  71. * Specifies the line end decoration, such as a triangle or arrowhead.
  72. */
  73. public void setLineHeadDecoration(LineDecoration style) {
  74. CTLineProperties ln = getSpPr().getLn();
  75. CTLineEndProperties lnEnd = ln.isSetHeadEnd() ? ln.getHeadEnd() : ln.addNewHeadEnd();
  76. if (style == null) {
  77. if (lnEnd.isSetType()) lnEnd.unsetType();
  78. } else {
  79. lnEnd.setType(STLineEndType.Enum.forInt(style.ordinal() + 1));
  80. }
  81. }
  82. public LineDecoration getLineHeadDecoration() {
  83. CTLineProperties ln = getSpPr().getLn();
  84. if (ln == null || !ln.isSetHeadEnd()) return LineDecoration.NONE;
  85. STLineEndType.Enum end = ln.getHeadEnd().getType();
  86. return end == null ? LineDecoration.NONE : LineDecoration.values()[end.intValue() - 1];
  87. }
  88. /**
  89. * specifies decorations which can be added to the head of a line.
  90. */
  91. public void setLineHeadWidth(LineEndWidth style) {
  92. CTLineProperties ln = getSpPr().getLn();
  93. CTLineEndProperties lnEnd = ln.isSetHeadEnd() ? ln.getHeadEnd() : ln.addNewHeadEnd();
  94. if (style == null) {
  95. if (lnEnd.isSetW()) lnEnd.unsetW();
  96. } else {
  97. lnEnd.setW(STLineEndWidth.Enum.forInt(style.ordinal() + 1));
  98. }
  99. }
  100. public LineEndWidth getLineHeadWidth() {
  101. CTLineProperties ln = getSpPr().getLn();
  102. if (ln == null || !ln.isSetHeadEnd()) return LineEndWidth.MEDIUM;
  103. STLineEndWidth.Enum w = ln.getHeadEnd().getW();
  104. return w == null ? LineEndWidth.MEDIUM : LineEndWidth.values()[w.intValue() - 1];
  105. }
  106. /**
  107. * Specifies the line end width in relation to the line width.
  108. */
  109. public void setLineHeadLength(LineEndLength style) {
  110. CTLineProperties ln = getSpPr().getLn();
  111. CTLineEndProperties lnEnd = ln.isSetHeadEnd() ? ln.getHeadEnd() : ln.addNewHeadEnd();
  112. if (style == null) {
  113. if (lnEnd.isSetLen()) lnEnd.unsetLen();
  114. } else {
  115. lnEnd.setLen(STLineEndLength.Enum.forInt(style.ordinal() + 1));
  116. }
  117. }
  118. public LineEndLength getLineHeadLength() {
  119. CTLineProperties ln = getSpPr().getLn();
  120. if (ln == null || !ln.isSetHeadEnd()) return LineEndLength.MEDIUM;
  121. STLineEndLength.Enum len = ln.getHeadEnd().getLen();
  122. return len == null ? LineEndLength.MEDIUM : LineEndLength.values()[len.intValue() - 1];
  123. }
  124. /**
  125. * Specifies the line end decoration, such as a triangle or arrowhead.
  126. */
  127. public void setLineTailDecoration(LineDecoration style) {
  128. CTLineProperties ln = getSpPr().getLn();
  129. CTLineEndProperties lnEnd = ln.isSetTailEnd() ? ln.getTailEnd() : ln.addNewTailEnd();
  130. if (style == null) {
  131. if (lnEnd.isSetType()) lnEnd.unsetType();
  132. } else {
  133. lnEnd.setType(STLineEndType.Enum.forInt(style.ordinal() + 1));
  134. }
  135. }
  136. public LineDecoration getLineTailDecoration() {
  137. CTLineProperties ln = getSpPr().getLn();
  138. if (ln == null || !ln.isSetTailEnd()) return LineDecoration.NONE;
  139. STLineEndType.Enum end = ln.getTailEnd().getType();
  140. return end == null ? LineDecoration.NONE : LineDecoration.values()[end.intValue() - 1];
  141. }
  142. /**
  143. * specifies decorations which can be added to the tail of a line.
  144. */
  145. public void setLineTailWidth(LineEndWidth style) {
  146. CTLineProperties ln = getSpPr().getLn();
  147. CTLineEndProperties lnEnd = ln.isSetTailEnd() ? ln.getTailEnd() : ln.addNewTailEnd();
  148. if (style == null) {
  149. if (lnEnd.isSetW()) lnEnd.unsetW();
  150. } else {
  151. lnEnd.setW(STLineEndWidth.Enum.forInt(style.ordinal() + 1));
  152. }
  153. }
  154. public LineEndWidth getLineTailWidth() {
  155. CTLineProperties ln = getSpPr().getLn();
  156. if (ln == null || !ln.isSetTailEnd()) return LineEndWidth.MEDIUM;
  157. STLineEndWidth.Enum w = ln.getTailEnd().getW();
  158. return w == null ? LineEndWidth.MEDIUM : LineEndWidth.values()[w.intValue() - 1];
  159. }
  160. /**
  161. * Specifies the line end width in relation to the line width.
  162. */
  163. public void setLineTailLength(LineEndLength style) {
  164. CTLineProperties ln = getSpPr().getLn();
  165. CTLineEndProperties lnEnd = ln.isSetTailEnd() ? ln.getTailEnd() : ln.addNewTailEnd();
  166. if (style == null) {
  167. if (lnEnd.isSetLen()) lnEnd.unsetLen();
  168. } else {
  169. lnEnd.setLen(STLineEndLength.Enum.forInt(style.ordinal() + 1));
  170. }
  171. }
  172. public LineEndLength getLineTailLength() {
  173. CTLineProperties ln = getSpPr().getLn();
  174. if (ln == null || !ln.isSetTailEnd()) return LineEndLength.MEDIUM;
  175. STLineEndLength.Enum len = ln.getTailEnd().getLen();
  176. return len == null ? LineEndLength.MEDIUM : LineEndLength.values()[len.intValue() - 1];
  177. }
  178. Outline getTailDecoration() {
  179. LineEndLength tailLength = getLineTailLength();
  180. LineEndWidth tailWidth = getLineTailWidth();
  181. double lineWidth = getLineWidth();
  182. Rectangle2D anchor = getAnchor();
  183. double x2 = anchor.getX() + anchor.getWidth(),
  184. y2 = anchor.getY() + anchor.getHeight();
  185. double alpha = Math.atan(anchor.getHeight() / anchor.getWidth());
  186. AffineTransform at = new AffineTransform();
  187. Shape shape = null;
  188. Path p = null;
  189. Rectangle2D bounds;
  190. double scaleY = Math.pow(2, tailWidth.ordinal());
  191. double scaleX = Math.pow(2, tailLength.ordinal());
  192. switch (getLineTailDecoration()) {
  193. case OVAL:
  194. p = new Path();
  195. shape = new Ellipse2D.Double(0, 0, lineWidth * scaleX, lineWidth * scaleY);
  196. bounds = shape.getBounds2D();
  197. at.translate(x2 - bounds.getWidth() / 2, y2 - bounds.getHeight() / 2);
  198. at.rotate(alpha, bounds.getX() + bounds.getWidth() / 2, bounds.getY() + bounds.getHeight() / 2);
  199. break;
  200. case ARROW:
  201. p = new Path();
  202. GeneralPath arrow = new GeneralPath();
  203. arrow.moveTo((float) (-lineWidth * 3), (float) (-lineWidth * 2));
  204. arrow.lineTo(0, 0);
  205. arrow.lineTo((float) (-lineWidth * 3), (float) (lineWidth * 2));
  206. shape = arrow;
  207. at.translate(x2, y2);
  208. at.rotate(alpha);
  209. break;
  210. case TRIANGLE:
  211. p = new Path();
  212. scaleY = tailWidth.ordinal() + 1;
  213. scaleX = tailLength.ordinal() + 1;
  214. GeneralPath triangle = new GeneralPath();
  215. triangle.moveTo((float) (-lineWidth * scaleX), (float) (-lineWidth * scaleY / 2));
  216. triangle.lineTo(0, 0);
  217. triangle.lineTo((float) (-lineWidth * scaleX), (float) (lineWidth * scaleY / 2));
  218. triangle.closePath();
  219. shape = triangle;
  220. at.translate(x2, y2);
  221. at.rotate(alpha);
  222. break;
  223. default:
  224. break;
  225. }
  226. if (shape != null) {
  227. shape = at.createTransformedShape(shape);
  228. }
  229. return shape == null ? null : new Outline(shape, p);
  230. }
  231. Outline getHeadDecoration() {
  232. LineEndLength headLength = getLineHeadLength();
  233. LineEndWidth headWidth = getLineHeadWidth();
  234. double lineWidth = getLineWidth();
  235. Rectangle2D anchor = getAnchor();
  236. double x1 = anchor.getX(),
  237. y1 = anchor.getY();
  238. double alpha = Math.atan(anchor.getHeight() / anchor.getWidth());
  239. AffineTransform at = new AffineTransform();
  240. Shape shape = null;
  241. Path p = null;
  242. Rectangle2D bounds;
  243. double scaleY = 1;
  244. double scaleX = 1;
  245. switch (getLineHeadDecoration()) {
  246. case OVAL:
  247. p = new Path();
  248. shape = new Ellipse2D.Double(0, 0, lineWidth * scaleX, lineWidth * scaleY);
  249. bounds = shape.getBounds2D();
  250. at.translate(x1 - bounds.getWidth() / 2, y1 - bounds.getHeight() / 2);
  251. at.rotate(alpha, bounds.getX() + bounds.getWidth() / 2, bounds.getY() + bounds.getHeight() / 2);
  252. break;
  253. case STEALTH:
  254. case ARROW:
  255. p = new Path();
  256. GeneralPath arrow = new GeneralPath();
  257. arrow.moveTo((float) (lineWidth * 3 * scaleX), (float) (-lineWidth * scaleY * 2));
  258. arrow.lineTo(0, 0);
  259. arrow.lineTo((float) (lineWidth * 3 * scaleX), (float) (lineWidth * scaleY * 2));
  260. shape = arrow;
  261. at.translate(x1, y1);
  262. at.rotate(alpha);
  263. break;
  264. case TRIANGLE:
  265. p = new Path();
  266. scaleY = headWidth.ordinal() + 1;
  267. scaleX = headLength.ordinal() + 1;
  268. GeneralPath triangle = new GeneralPath();
  269. triangle.moveTo((float) (lineWidth * scaleX), (float) (-lineWidth * scaleY / 2));
  270. triangle.lineTo(0, 0);
  271. triangle.lineTo((float) (lineWidth * scaleX), (float) (lineWidth * scaleY / 2));
  272. triangle.closePath();
  273. shape = triangle;
  274. at.translate(x1, y1);
  275. at.rotate(alpha);
  276. break;
  277. default:
  278. break;
  279. }
  280. if (shape != null) {
  281. shape = at.createTransformedShape(shape);
  282. }
  283. return shape == null ? null : new Outline(shape, p);
  284. }
  285. @Override
  286. List<Outline> getCustomOutlines(){
  287. List<Outline> lst = new ArrayList<Outline>();
  288. Outline head = getHeadDecoration();
  289. if(head != null) lst.add(head);
  290. Outline tail = getTailDecoration();
  291. if(tail != null) lst.add(tail);
  292. return lst;
  293. }
  294. /**
  295. * YK: dashing of lines is suppressed for now.
  296. * @return
  297. */
  298. @Override
  299. public XSLFShadow getShadow() {
  300. return null;
  301. }
  302. }