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.

PathGradientPaint.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.AlphaComposite;
  17. import java.awt.BasicStroke;
  18. import java.awt.Color;
  19. import java.awt.Graphics2D;
  20. import java.awt.LinearGradientPaint;
  21. import java.awt.MultipleGradientPaint.ColorSpaceType;
  22. import java.awt.MultipleGradientPaint.CycleMethod;
  23. import java.awt.Paint;
  24. import java.awt.PaintContext;
  25. import java.awt.Rectangle;
  26. import java.awt.RenderingHints;
  27. import java.awt.Shape;
  28. import java.awt.geom.AffineTransform;
  29. import java.awt.geom.Area;
  30. import java.awt.geom.IllegalPathStateException;
  31. import java.awt.geom.Point2D;
  32. import java.awt.geom.Rectangle2D;
  33. import java.awt.image.BufferedImage;
  34. import java.awt.image.ColorModel;
  35. import java.awt.image.Raster;
  36. import java.awt.image.WritableRaster;
  37. import org.apache.poi.util.Internal;
  38. @Internal
  39. public class PathGradientPaint implements Paint {
  40. // http://asserttrue.blogspot.de/2010/01/how-to-iimplement-custom-paint-in-50.html
  41. private final Color[] colors;
  42. private final float[] fractions;
  43. private final int capStyle;
  44. private final int joinStyle;
  45. private final int transparency;
  46. PathGradientPaint(float[] fractions, Color[] colors) {
  47. this(fractions,colors,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
  48. }
  49. private PathGradientPaint(float[] fractions, Color[] colors, int capStyle, int joinStyle) {
  50. this.colors = colors.clone();
  51. this.fractions = fractions.clone();
  52. this.capStyle = capStyle;
  53. this.joinStyle = joinStyle;
  54. // determine transparency
  55. boolean opaque = true;
  56. for (Color c : colors) {
  57. if (c != null) {
  58. opaque = opaque && (c.getAlpha() == 0xff);
  59. }
  60. }
  61. this.transparency = opaque ? OPAQUE : TRANSLUCENT;
  62. }
  63. @Override
  64. public PathGradientContext createContext(ColorModel cm,
  65. Rectangle deviceBounds,
  66. Rectangle2D userBounds,
  67. AffineTransform transform,
  68. RenderingHints hints) {
  69. return new PathGradientContext(cm, deviceBounds, userBounds, transform, hints);
  70. }
  71. public int getTransparency() {
  72. return transparency;
  73. }
  74. public class PathGradientContext implements PaintContext {
  75. final Rectangle deviceBounds;
  76. final Rectangle2D userBounds;
  77. protected final AffineTransform xform;
  78. final RenderingHints hints;
  79. /**
  80. * for POI: the shape will be only known when the subclasses determines the concrete implementation
  81. * in the draw/-content method, so we need to postpone the setting/creation as long as possible
  82. **/
  83. protected final Shape shape;
  84. final PaintContext pCtx;
  85. final int gradientSteps;
  86. WritableRaster raster;
  87. PathGradientContext(
  88. ColorModel cm
  89. , Rectangle deviceBounds
  90. , Rectangle2D userBounds
  91. , AffineTransform xform
  92. , RenderingHints hints
  93. ) {
  94. shape = (Shape)hints.get(Drawable.GRADIENT_SHAPE);
  95. if (shape == null) {
  96. throw new IllegalPathStateException("PathGradientPaint needs a shape to be set via the rendering hint Drawable.GRADIANT_SHAPE.");
  97. }
  98. this.deviceBounds = deviceBounds;
  99. this.userBounds = userBounds;
  100. this.xform = xform;
  101. this.hints = hints;
  102. gradientSteps = getGradientSteps(shape);
  103. Point2D start = new Point2D.Double(0, 0);
  104. Point2D end = new Point2D.Double(gradientSteps, 0);
  105. LinearGradientPaint gradientPaint = new LinearGradientPaint(start, end, fractions, colors, CycleMethod.NO_CYCLE, ColorSpaceType.SRGB, new AffineTransform());
  106. Rectangle bounds = new Rectangle(0, 0, gradientSteps, 1);
  107. pCtx = gradientPaint.createContext(cm, bounds, bounds, new AffineTransform(), hints);
  108. }
  109. public void dispose() {}
  110. public ColorModel getColorModel() {
  111. return pCtx.getColorModel();
  112. }
  113. public Raster getRaster(int xOffset, int yOffset, int w, int h) {
  114. ColorModel cm = getColorModel();
  115. raster = createRaster();
  116. // TODO: eventually use caching here
  117. WritableRaster childRaster = cm.createCompatibleWritableRaster(w, h);
  118. Rectangle2D childRect = new Rectangle2D.Double(xOffset, yOffset, w, h);
  119. if (!childRect.intersects(deviceBounds)) {
  120. // usually doesn't happen ...
  121. return childRaster;
  122. }
  123. Rectangle2D destRect = new Rectangle2D.Double();
  124. Rectangle2D.intersect(childRect, deviceBounds, destRect);
  125. int dx = (int)(destRect.getX()-deviceBounds.getX());
  126. int dy = (int)(destRect.getY()-deviceBounds.getY());
  127. int dw = (int)destRect.getWidth();
  128. int dh = (int)destRect.getHeight();
  129. Object data = raster.getDataElements(dx, dy, dw, dh, null);
  130. dx = (int)(destRect.getX()-childRect.getX());
  131. dy = (int)(destRect.getY()-childRect.getY());
  132. childRaster.setDataElements(dx, dy, dw, dh, data);
  133. return childRaster;
  134. }
  135. int getGradientSteps(Shape gradientShape) {
  136. Rectangle rect = gradientShape.getBounds();
  137. int lower = 1;
  138. int upper = (int)(Math.max(rect.getWidth(),rect.getHeight())/2.0);
  139. while (lower < upper-1) {
  140. int mid = lower + (upper - lower) / 2;
  141. BasicStroke bs = new BasicStroke(mid, capStyle, joinStyle);
  142. Area area = new Area(bs.createStrokedShape(gradientShape));
  143. if (area.isSingular()) {
  144. upper = mid;
  145. } else {
  146. lower = mid;
  147. }
  148. }
  149. // always report at least one gradient step
  150. return Math.max(upper, 1);
  151. }
  152. public WritableRaster createRaster() {
  153. if (raster != null) {
  154. return raster;
  155. }
  156. ColorModel cm = getColorModel();
  157. raster = cm.createCompatibleWritableRaster((int)deviceBounds.getWidth(), (int)deviceBounds.getHeight());
  158. BufferedImage img = new BufferedImage(cm, raster, false, null);
  159. Graphics2D graphics = img.createGraphics();
  160. graphics.setRenderingHints(hints);
  161. graphics.translate(-deviceBounds.getX(), -deviceBounds.getY());
  162. graphics.transform(xform);
  163. Raster img2 = pCtx.getRaster(0, 0, gradientSteps, 1);
  164. int[] rgb = new int[cm.getNumComponents()];
  165. for (int i = gradientSteps-1; i>=0; i--) {
  166. img2.getPixel(gradientSteps-i-1, 0, rgb);
  167. Color c = new Color(rgb[0],rgb[1],rgb[2]);
  168. if (rgb.length == 4) {
  169. // it doesn't work to use just a color with transparency ...
  170. graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, rgb[3]/255.0f));
  171. }
  172. graphics.setStroke(new BasicStroke(i+1F, capStyle, joinStyle));
  173. graphics.setColor(c);
  174. if (i == gradientSteps-1) {
  175. graphics.fill(shape);
  176. }
  177. graphics.draw(shape);
  178. }
  179. graphics.dispose();
  180. return raster;
  181. }
  182. }
  183. }