Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DrawShape.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. protected static boolean isHSLF(Shape<?,?> shape) {
  39. return shape.getClass().getCanonicalName().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 whos transform matrix will be modified
  45. */
  46. @Override
  47. public void applyTransform(Graphics2D graphics) {
  48. if (!(shape instanceof PlaceableShape)) {
  49. return;
  50. }
  51. PlaceableShape<?,?> ps = (PlaceableShape<?,?>)shape;
  52. final boolean isHSLF = isHSLF(shape);
  53. AffineTransform tx = (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM);
  54. if (tx == null) {
  55. tx = new AffineTransform();
  56. }
  57. // we saw one document failing here, probably the format is slightly broken, but
  58. // maybe better to try to handle it more gracefully
  59. java.awt.Shape transformedShape = tx.createTransformedShape(ps.getAnchor());
  60. if(transformedShape == null) {
  61. return;
  62. }
  63. final Rectangle2D anchor = transformedShape.getBounds2D();
  64. char cmds[] = isHSLF ? new char[]{ 'h','v','r' } : new char[]{ 'r','h','v' };
  65. for (char ch : cmds) {
  66. switch (ch) {
  67. case 'h':
  68. //flip horizontal
  69. if (ps.getFlipHorizontal()) {
  70. graphics.translate(anchor.getX() + anchor.getWidth(), anchor.getY());
  71. graphics.scale(-1, 1);
  72. graphics.translate(-anchor.getX(), -anchor.getY());
  73. }
  74. break;
  75. case 'v':
  76. //flip vertical
  77. if (ps.getFlipVertical()) {
  78. graphics.translate(anchor.getX(), anchor.getY() + anchor.getHeight());
  79. graphics.scale(1, -1);
  80. graphics.translate(-anchor.getX(), -anchor.getY());
  81. }
  82. break;
  83. case 'r':
  84. // rotation
  85. double rotation = ps.getRotation();
  86. if (rotation != 0.) {
  87. // PowerPoint rotates shapes relative to the geometric center
  88. double centerX = anchor.getCenterX();
  89. double centerY = anchor.getCenterY();
  90. // normalize rotation
  91. rotation %= 360.;
  92. if (rotation < 0) {
  93. rotation += 360.;
  94. }
  95. int quadrant = (((int)rotation+45)/90)%4;
  96. double scaleX = 1.0, scaleY = 1.0;
  97. // scale to bounding box (bug #53176)
  98. if (quadrant == 1 || quadrant == 3) {
  99. // In quadrant 1 and 3, which is basically a shape in a more or less portrait orientation
  100. // (45-135 degrees and 225-315 degrees), we need to first rotate the shape by a multiple
  101. // of 90 degrees and then resize the bounding box to its original bbox. After that we can
  102. // rotate the shape to the exact rotation amount.
  103. // It's strange that you'll need to rotate the shape back and forth again, but you can
  104. // think of it, as if you paint the shape on a canvas. First you rotate the canvas, which might
  105. // be already (differently) scaled, so you can paint the shape in its default orientation
  106. // and later on, turn it around again to compare it with its original size ...
  107. AffineTransform txs;
  108. if (isHSLF) {
  109. txs = new AffineTransform(tx);
  110. } else {
  111. // this handling is only based on try and error ... not sure why xslf is handled differently.
  112. txs = new AffineTransform();
  113. txs.translate(centerX, centerY);
  114. txs.rotate(Math.PI/2.); // actually doesn't matter if +/- 90 degrees
  115. txs.translate(-centerX, -centerY);
  116. txs.concatenate(tx);
  117. }
  118. txs.translate(centerX, centerY);
  119. txs.rotate(Math.PI/2.);
  120. txs.translate(-centerX, -centerY);
  121. Rectangle2D anchor2 = txs.createTransformedShape(ps.getAnchor()).getBounds2D();
  122. scaleX = safeScale(anchor.getWidth(), anchor2.getWidth());
  123. scaleY = safeScale(anchor.getHeight(), anchor2.getHeight());
  124. } else {
  125. quadrant = 0;
  126. }
  127. // transformation is applied reversed ...
  128. graphics.translate(centerX, centerY);
  129. double rot = Math.toRadians(rotation-quadrant*90.);
  130. if (rot != 0) {
  131. graphics.rotate(rot);
  132. }
  133. graphics.scale(scaleX, scaleY);
  134. rot = Math.toRadians(quadrant*90.);
  135. if (rot != 0) {
  136. graphics.rotate(rot);
  137. }
  138. graphics.translate(-centerX, -centerY);
  139. }
  140. break;
  141. default:
  142. throw new RuntimeException("unexpected transform code " + ch);
  143. }
  144. }
  145. }
  146. private static double safeScale(double dim1, double dim2) {
  147. if (dim1 == 0.) {
  148. return 1;
  149. }
  150. return (dim2 == 0.) ? 1 : dim1/dim2;
  151. }
  152. @Override
  153. public void draw(Graphics2D graphics) {
  154. }
  155. @Override
  156. public void drawContent(Graphics2D graphics) {
  157. }
  158. public static Rectangle2D getAnchor(Graphics2D graphics, PlaceableShape<?,?> shape) {
  159. return getAnchor(graphics, shape.getAnchor());
  160. }
  161. public static Rectangle2D getAnchor(Graphics2D graphics, Rectangle2D anchor) {
  162. if(graphics == null) {
  163. return anchor;
  164. }
  165. AffineTransform tx = (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM);
  166. if(tx != null && !tx.isIdentity() && tx.createTransformedShape(anchor) != null) {
  167. anchor = tx.createTransformedShape(anchor).getBounds2D();
  168. }
  169. return anchor;
  170. }
  171. protected Shape<?,?> getShape() {
  172. return shape;
  173. }
  174. protected static BasicStroke getStroke(StrokeStyle strokeStyle) {
  175. float lineWidth = (float) strokeStyle.getLineWidth();
  176. if (lineWidth == 0.0f) {
  177. // Both PowerPoint and OOo draw zero-length lines as 0.25pt
  178. lineWidth = 0.25f;
  179. }
  180. LineDash lineDash = strokeStyle.getLineDash();
  181. if (lineDash == null) {
  182. lineDash = LineDash.SOLID;
  183. }
  184. int dashPatI[] = lineDash.pattern;
  185. final float dash_phase = 0;
  186. float[] dashPatF = null;
  187. if (dashPatI != null) {
  188. dashPatF = new float[dashPatI.length];
  189. for (int i=0; i<dashPatI.length; i++) {
  190. dashPatF[i] = dashPatI[i]*Math.max(1, lineWidth);
  191. }
  192. }
  193. LineCap lineCapE = strokeStyle.getLineCap();
  194. if (lineCapE == null) {
  195. lineCapE = LineCap.FLAT;
  196. }
  197. int lineCap;
  198. switch (lineCapE) {
  199. case ROUND:
  200. lineCap = BasicStroke.CAP_ROUND;
  201. break;
  202. case SQUARE:
  203. lineCap = BasicStroke.CAP_SQUARE;
  204. break;
  205. default:
  206. case FLAT:
  207. lineCap = BasicStroke.CAP_BUTT;
  208. break;
  209. }
  210. int lineJoin = BasicStroke.JOIN_ROUND;
  211. return new BasicStroke(lineWidth, lineCap, lineJoin, lineWidth, dashPatF, dash_phase);
  212. }
  213. }