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.

XSLFShape.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.xmlbeans.XmlObject;
  22. import java.awt.Graphics2D;
  23. import java.awt.geom.Rectangle2D;
  24. /**
  25. * Base super-class class for all shapes in PresentationML
  26. *
  27. * @author Yegor Kozlov
  28. */
  29. @Beta
  30. public abstract class XSLFShape {
  31. /**
  32. *
  33. * @return the position of this shape within the drawing canvas.
  34. * The coordinates are expressed in points
  35. */
  36. public abstract Rectangle2D getAnchor();
  37. /**
  38. *
  39. * @param anchor the position of this shape within the drawing canvas.
  40. * The coordinates are expressed in points
  41. */
  42. public abstract void setAnchor(Rectangle2D anchor);
  43. /**
  44. *
  45. * @return the xml bean holding this shape's data
  46. */
  47. public abstract XmlObject getXmlObject();
  48. /**
  49. *
  50. * @return human-readable name of this shape, e.g. "Rectange 3"
  51. */
  52. public abstract String getShapeName();
  53. /**
  54. * Returns a unique identifier for this shape within the current document.
  55. * This ID may be used to assist in uniquely identifying this object so that it can
  56. * be referred to by other parts of the document.
  57. * <p>
  58. * If multiple objects within the same document share the same id attribute value,
  59. * then the document shall be considered non-conformant.
  60. * </p>
  61. *
  62. * @return unique id of this shape
  63. */
  64. public abstract int getShapeId();
  65. /**
  66. * Rotate this shape.
  67. * <p>
  68. * Positive angles are clockwise (i.e., towards the positive y axis);
  69. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  70. * </p>
  71. *
  72. * @param theta the rotation angle in degrees.
  73. */
  74. public abstract void setRotation(double theta);
  75. /**
  76. * Rotation angle in degrees
  77. * <p>
  78. * Positive angles are clockwise (i.e., towards the positive y axis);
  79. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  80. * </p>
  81. *
  82. * @return rotation angle in degrees
  83. */
  84. public abstract double getRotation();
  85. /**
  86. * @param flip whether the shape is horizontally flipped
  87. */
  88. public abstract void setFlipHorizontal(boolean flip);
  89. /**
  90. * Whether the shape is vertically flipped
  91. *
  92. * @param flip whether the shape is vertically flipped
  93. */
  94. public abstract void setFlipVertical(boolean flip);
  95. /**
  96. * Whether the shape is horizontally flipped
  97. *
  98. * @return whether the shape is horizontally flipped
  99. */
  100. public abstract boolean getFlipHorizontal();
  101. /**
  102. * Whether the shape is vertically flipped
  103. *
  104. * @return whether the shape is vertically flipped
  105. */
  106. public abstract boolean getFlipVertical();
  107. /**
  108. * Draw this shape into the supplied canvas
  109. *
  110. * @param graphics the graphics to draw into
  111. */
  112. public abstract void draw(Graphics2D graphics);
  113. /**
  114. * Apply 2-D transforms before drawing this shape. This includes rotation and flipping.
  115. *
  116. * @param graphics the graphics whos transform matrix will be modified
  117. */
  118. protected void applyTransform(Graphics2D graphics){
  119. Rectangle2D anchor = getAnchor();
  120. // rotation
  121. double rotation = getRotation();
  122. if(rotation != 0.) {
  123. // PowerPoint rotates shapes relative to the geometric center
  124. double centerX = anchor.getX() + anchor.getWidth()/2;
  125. double centerY = anchor.getY() + anchor.getHeight()/2;
  126. graphics.translate(centerX, centerY);
  127. graphics.rotate(Math.toRadians(rotation));
  128. graphics.translate(-centerX, -centerY);
  129. }
  130. //flip horizontal
  131. if(getFlipHorizontal()){
  132. graphics.translate(anchor.getX() + anchor.getWidth(), anchor.getY());
  133. graphics.scale(-1, 1);
  134. graphics.translate(-anchor.getX() , -anchor.getY());
  135. }
  136. //flip vertical
  137. if(getFlipVertical()){
  138. graphics.translate(anchor.getX(), anchor.getY() + anchor.getHeight());
  139. graphics.scale(1, -1);
  140. graphics.translate(-anchor.getX(), -anchor.getY());
  141. }
  142. }
  143. }