Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

XSLFShadow.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 angle = getAngle();
  36. double dist = getDistance();
  37. double dx = dist * Math.cos(Math.toRadians(angle));
  38. double dy = dist * Math.sin(Math.toRadians(angle));
  39. graphics.translate(dx, dy);
  40. Color fillColor = getFillColor();
  41. if (fillColor != null) {
  42. graphics.setColor(fillColor);
  43. graphics.fill(outline);
  44. }
  45. graphics.translate(-dx, -dy);
  46. }
  47. public void draw(Graphics2D graphics, Shape outline) {
  48. double angle = getAngle();
  49. double dist = getDistance();
  50. double dx = dist * Math.cos(Math.toRadians(angle));
  51. double dy = dist * Math.sin(Math.toRadians(angle));
  52. graphics.translate(dx, dy);
  53. Color fillColor = getFillColor();
  54. if (fillColor != null) {
  55. graphics.setColor(fillColor);
  56. graphics.draw(outline);
  57. }
  58. graphics.translate(-dx, -dy);
  59. }
  60. @Override
  61. public Rectangle2D getAnchor(){
  62. return _parent.getAnchor();
  63. }
  64. @Override
  65. public void setAnchor(Rectangle2D anchor){
  66. throw new IllegalStateException("You can't set anchor of a shadow");
  67. }
  68. /**
  69. * @return the offset of this shadow in points
  70. */
  71. public double getDistance(){
  72. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  73. return ct.isSetDist() ? Units.toPoints(ct.getDist()) : 0;
  74. }
  75. /**
  76. *
  77. * @return the direction to offset the shadow in angles
  78. */
  79. public double getAngle(){
  80. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  81. return ct.isSetDir() ? (double)ct.getDir() / 60000 : 0;
  82. }
  83. /**
  84. *
  85. * @return the blur radius of the shadow
  86. * TODO: figure out how to make sense of this property when rendering shadows
  87. */
  88. public double getBlur(){
  89. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  90. return ct.isSetBlurRad() ? Units.toPoints(ct.getBlurRad()) : 0;
  91. }
  92. /**
  93. * @return the color of this shadow.
  94. * Depending whether the parent shape is filled or stroked, this color is used to fill or stroke this shadow
  95. */
  96. @Override
  97. public Color getFillColor() {
  98. XSLFTheme theme = getSheet().getTheme();
  99. CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
  100. if(ct == null) {
  101. return null;
  102. } else {
  103. CTSchemeColor phClr = ct.getSchemeClr();
  104. return new XSLFColor(ct, theme, phClr).getColor();
  105. }
  106. }
  107. }