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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.sl.draw;
  16. import java.awt.BasicStroke;
  17. import java.awt.Graphics2D;
  18. import java.awt.geom.AffineTransform;
  19. import java.awt.geom.Rectangle2D;
  20. import java.util.Locale;
  21. import org.apache.poi.sl.usermodel.PlaceableShape;
  22. import org.apache.poi.sl.usermodel.Shape;
  23. import org.apache.poi.sl.usermodel.StrokeStyle;
  24. import org.apache.poi.sl.usermodel.StrokeStyle.LineCap;
  25. import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
  26. public class DrawShape implements Drawable {
  27. protected final Shape<?,?> shape;
  28. public DrawShape(Shape<?,?> shape) {
  29. this.shape = shape;
  30. }
  31. /**
  32. * Sometimes it's necessary to distinguish between XSLF/HSLF for the rendering.
  33. * Use this method on the shape to determine, if we work on the BIFF implementation
  34. *
  35. * @param shape the shape to render
  36. * @return {@code true} if HSLF implementation is used
  37. */
  38. static boolean isHSLF(Object shape) {
  39. return shape.getClass().getName().toLowerCase(Locale.ROOT).contains("hslf");
  40. }
  41. /**
  42. * Apply 2-D transforms before drawing this shape. This includes rotation and flipping.
  43. *
  44. * @param graphics the graphics whose transform matrix will be modified
  45. */
  46. @Override
  47. public void applyTransform(Graphics2D graphics) {
  48. if (!(shape instanceof PlaceableShape) || graphics == null) {
  49. return;
  50. }
  51. final Rectangle2D anchor = getAnchor(graphics, (PlaceableShape<?,?>)shape);
  52. if (anchor == null) {
  53. return;
  54. }
  55. if (isHSLF(shape)) {
  56. flipHorizontal(graphics, anchor);
  57. flipVertical(graphics, anchor);
  58. rotate(graphics, anchor);
  59. } else {
  60. rotate(graphics, anchor);
  61. flipHorizontal(graphics, anchor);
  62. flipVertical(graphics, anchor);
  63. }
  64. }
  65. private void flipHorizontal(Graphics2D graphics, Rectangle2D anchor) {
  66. assert(shape instanceof PlaceableShape && anchor != null);
  67. if (((PlaceableShape<?,?>)shape).getFlipHorizontal()) {
  68. graphics.translate(anchor.getX() + anchor.getWidth(), anchor.getY());
  69. graphics.scale(-1, 1);
  70. graphics.translate(-anchor.getX(), -anchor.getY());
  71. }
  72. }
  73. private void flipVertical(Graphics2D graphics, Rectangle2D anchor) {
  74. assert(shape instanceof PlaceableShape && anchor != null);
  75. if (((PlaceableShape<?,?>)shape).getFlipVertical()) {
  76. graphics.translate(anchor.getX(), anchor.getY() + anchor.getHeight());
  77. graphics.scale(1, -1);
  78. graphics.translate(-anchor.getX(), -anchor.getY());
  79. }
  80. }
  81. private void rotate(Graphics2D graphics, Rectangle2D anchor) {
  82. assert(shape instanceof PlaceableShape && anchor != null);
  83. double rotation = ((PlaceableShape<?,?>)shape).getRotation();
  84. if (rotation != 0.) {
  85. // PowerPoint rotates shapes relative to the geometric center
  86. graphics.rotate(Math.toRadians(rotation), anchor.getCenterX(), anchor.getCenterY());
  87. }
  88. }
  89. private static double safeScale(double dim1, double dim2) {
  90. return (dim1 == 0. || dim2 == 0.) ? 1 : dim1/dim2;
  91. }
  92. @Override
  93. public void draw(Graphics2D graphics) {
  94. }
  95. @Override
  96. public void drawContent(Graphics2D graphics) {
  97. }
  98. public static Rectangle2D getAnchor(Graphics2D graphics, PlaceableShape<?,?> shape) {
  99. final Rectangle2D shapeAnchor = shape.getAnchor();
  100. if (shapeAnchor == null) {
  101. return null;
  102. }
  103. final boolean isHSLF = isHSLF(shape);
  104. AffineTransform tx = graphics == null ? null : (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM);
  105. if (tx == null) {
  106. tx = new AffineTransform();
  107. }
  108. final double rotation = ((shape.getRotation() % 360.) + 360.) % 360.;
  109. final int quadrant = (((int)rotation+45)/90)%4;
  110. final Rectangle2D normalizedShape;
  111. // scale to bounding box (bug #53176)
  112. if (quadrant == 1 || quadrant == 3) {
  113. // In a rotated quadrant 1 (=45-135 degrees) and 3 (=225-315 degrees), which is basically a shape in a
  114. // more or less portrait orientation, Powerpoint doesn't use the normal shape anchor,
  115. // but rotate it 90 degress and apply the group transformations.
  116. // We try to revert that distortion and return the normalized anchor.
  117. // It's strange that you'll need to rotate the shape back and forth again, but you can
  118. // think of it, as if you paint the shape on a canvas. First you rotate the canvas, which might
  119. // be already (differently) scaled, so you can paint the shape in its default orientation
  120. // and later on, turn it around again to compare it with its original size ...
  121. final Rectangle2D anchorO = tx.createTransformedShape(shapeAnchor).getBounds2D();
  122. final Rectangle2D anchorT;
  123. {
  124. final double centerX = anchorO.getCenterX();
  125. final double centerY = anchorO.getCenterY();
  126. final AffineTransform txs2 = new AffineTransform();
  127. // this handling is only based on try and error ... not sure why h/xslf is handled differently.
  128. if (!isHSLF) {
  129. txs2.quadrantRotate(1, centerX, centerY);
  130. txs2.concatenate(tx);
  131. }
  132. txs2.quadrantRotate(3, centerX, centerY);
  133. if (isHSLF) {
  134. txs2.concatenate(tx);
  135. }
  136. anchorT = txs2.createTransformedShape(shapeAnchor).getBounds2D();
  137. }
  138. final double scaleX2 = safeScale(anchorO.getWidth(), anchorT.getWidth());
  139. final double scaleY2 = safeScale(anchorO.getHeight(), anchorT.getHeight());
  140. {
  141. double centerX = shapeAnchor.getCenterX();
  142. double centerY = shapeAnchor.getCenterY();
  143. final AffineTransform txs2 = new AffineTransform();
  144. txs2.translate(centerX, centerY);
  145. // no need to rotate back and forth, just apply scaling inverted
  146. txs2.scale(scaleY2, scaleX2);
  147. txs2.translate(-centerX, -centerY);
  148. normalizedShape = txs2.createTransformedShape(shapeAnchor).getBounds2D();
  149. }
  150. } else {
  151. normalizedShape = shapeAnchor;
  152. }
  153. if (tx.isIdentity()) {
  154. return normalizedShape;
  155. }
  156. final java.awt.Shape anc = tx.createTransformedShape(normalizedShape);
  157. return (anc != null) ? anc.getBounds2D() : normalizedShape;
  158. }
  159. public static Rectangle2D getAnchor(Graphics2D graphics, Rectangle2D anchor) {
  160. if(graphics == null) {
  161. return anchor;
  162. }
  163. AffineTransform tx = (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM);
  164. if(tx != null && !tx.isIdentity() && tx.createTransformedShape(anchor) != null) {
  165. anchor = tx.createTransformedShape(anchor).getBounds2D();
  166. }
  167. return anchor;
  168. }
  169. protected Shape<?,?> getShape() {
  170. return shape;
  171. }
  172. protected static BasicStroke getStroke(StrokeStyle strokeStyle) {
  173. float lineWidth = (float) strokeStyle.getLineWidth();
  174. if (lineWidth == 0.0f) {
  175. // Both PowerPoint and OOo draw zero-length lines as 0.25pt
  176. lineWidth = 0.25f;
  177. }
  178. LineDash lineDash = strokeStyle.getLineDash();
  179. if (lineDash == null) {
  180. lineDash = LineDash.SOLID;
  181. }
  182. int[] dashPatI = lineDash.pattern;
  183. final float dash_phase = 0;
  184. float[] dashPatF = null;
  185. if (dashPatI != null) {
  186. dashPatF = new float[dashPatI.length];
  187. for (int i=0; i<dashPatI.length; i++) {
  188. dashPatF[i] = dashPatI[i]*Math.max(1, lineWidth);
  189. }
  190. }
  191. LineCap lineCapE = strokeStyle.getLineCap();
  192. if (lineCapE == null) {
  193. lineCapE = LineCap.FLAT;
  194. }
  195. int lineCap;
  196. switch (lineCapE) {
  197. case ROUND:
  198. lineCap = BasicStroke.CAP_ROUND;
  199. break;
  200. case SQUARE:
  201. lineCap = BasicStroke.CAP_SQUARE;
  202. break;
  203. default:
  204. case FLAT:
  205. lineCap = BasicStroke.CAP_BUTT;
  206. break;
  207. }
  208. int lineJoin = BasicStroke.JOIN_ROUND;
  209. return new BasicStroke(lineWidth, lineCap, lineJoin, 10, dashPatF, dash_phase);
  210. }
  211. }