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.

XSLFShadow.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 org.apache.poi.util.Units;
  17. import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
  18. import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
  19. import java.awt.Color;
  20. import java.awt.Graphics2D;
  21. import java.awt.Shape;
  22. import java.awt.geom.Rectangle2D;
  23. /**
  24. * Represents a shadow of a shape. For now supports only outer shadows.
  25. *
  26. * @author Yegor Kozlov
  27. */
  28. public class XSLFShadow extends XSLFSimpleShape {
  29. private XSLFSimpleShape _parent;
  30. /* package */XSLFShadow(CTOuterShadowEffect shape, XSLFSimpleShape parentShape) {
  31. super(shape, parentShape.getSheet());
  32. _parent = parentShape;
  33. }
  34. public void fill(Graphics2D graphics, Shape outline) {
  35. double shapeRotation = _parent.getRotation();
  36. if(_parent.getFlipVertical()){
  37. shapeRotation += 180;
  38. }
  39. double angle = getAngle() - shapeRotation;
  40. double dist = getDistance();
  41. double dx = dist * Math.cos(Math.toRadians(angle));
  42. double dy = dist * Math.sin(Math.toRadians(angle));
  43. graphics.translate(dx, dy);
  44. Color fillColor = getFillColor();
  45. if (fillColor != null) {
  46. graphics.setColor(fillColor);
  47. graphics.fill(outline);
  48. }
  49. graphics.translate(-dx, -dy);
  50. }
  51. public void draw(Graphics2D graphics, Shape outline) {
  52. double angle = getAngle();
  53. double dist = getDistance();
  54. double dx = dist * Math.cos(Math.toRadians(angle));
  55. double dy = dist * Math.sin(Math.toRadians(angle));
  56. graphics.translate(dx, dy);
  57. Color fillColor = getFillColor();
  58. if (fillColor != null) {
  59. graphics.setColor(fillColor);
  60. graphics.draw(outline);
  61. }
  62. graphics.translate(-dx, -dy);
  63. }
  64. @Override
  65. public Rectangle2D getAnchor(){
  66. return _parent.getAnchor();
  67. }
  68. @Override
  69. public void setAnchor(Rectangle2D anchor){
  70. throw new IllegalStateException("You can't set anchor of a shadow");
  71. }
  72. /**
  73. * @return the offset of this shadow in points
  74. */
  75. public double getDistance(){
  76. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  77. return ct.isSetDist() ? Units.toPoints(ct.getDist()) : 0;
  78. }
  79. /**
  80. *
  81. * @return the direction to offset the shadow in angles
  82. */
  83. public double getAngle(){
  84. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  85. return ct.isSetDir() ? (double)ct.getDir() / 60000 : 0;
  86. }
  87. /**
  88. *
  89. * @return the blur radius of the shadow
  90. * TODO: figure out how to make sense of this property when rendering shadows
  91. */
  92. public double getBlur(){
  93. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  94. return ct.isSetBlurRad() ? Units.toPoints(ct.getBlurRad()) : 0;
  95. }
  96. /**
  97. * @return the color of this shadow.
  98. * Depending whether the parent shape is filled or stroked, this color is used to fill or stroke this shadow
  99. */
  100. @Override
  101. public Color getFillColor() {
  102. XSLFTheme theme = getSheet().getTheme();
  103. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  104. if(ct == null) {
  105. return null;
  106. } else {
  107. CTSchemeColor phClr = ct.getSchemeClr();
  108. return new XSLFColor(ct, theme, phClr).getColor();
  109. }
  110. }
  111. }