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.

GradientFactory.java 9.4KB

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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.shading;
  19. import java.awt.Color;
  20. import java.awt.geom.AffineTransform;
  21. import java.awt.geom.Point2D;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.apache.batik.ext.awt.LinearGradientPaint;
  25. import org.apache.batik.ext.awt.MultipleGradientPaint;
  26. import org.apache.batik.ext.awt.RadialGradientPaint;
  27. import org.apache.xmlgraphics.java2d.color.ColorUtil;
  28. import org.apache.fop.pdf.PDFDeviceColorSpace;
  29. public abstract class GradientFactory<P extends Pattern> {
  30. public P createLinearGradient(LinearGradientPaint gp,
  31. AffineTransform baseTransform, AffineTransform transform) {
  32. Point2D startPoint = gp.getStartPoint();
  33. Point2D endPoint = gp.getEndPoint();
  34. List<Double> coords = new java.util.ArrayList<Double>(4);
  35. coords.add(new Double(startPoint.getX()));
  36. coords.add(new Double(startPoint.getY()));
  37. coords.add(new Double(endPoint.getX()));
  38. coords.add(new Double(endPoint.getY()));
  39. return createGradient(gp, coords, baseTransform, transform);
  40. }
  41. public P createRadialGradient(RadialGradientPaint gradient,
  42. AffineTransform baseTransform, AffineTransform transform) {
  43. double radius = gradient.getRadius();
  44. Point2D center = gradient.getCenterPoint();
  45. Point2D focus = gradient.getFocusPoint();
  46. double dx = focus.getX() - center.getX();
  47. double dy = focus.getY() - center.getY();
  48. double d = Math.sqrt(dx * dx + dy * dy);
  49. if (d > radius) {
  50. // The center point must be within the circle with
  51. // radius radius centered at center so limit it to that.
  52. double scale = (radius * .9999) / d;
  53. dx *= scale;
  54. dy *= scale;
  55. }
  56. List<Double> coords = new java.util.ArrayList<Double>();
  57. coords.add(Double.valueOf(center.getX() + dx));
  58. coords.add(Double.valueOf(center.getY() + dy));
  59. coords.add(Double.valueOf(0));
  60. coords.add(Double.valueOf(center.getX()));
  61. coords.add(Double.valueOf(center.getY()));
  62. coords.add(Double.valueOf(radius));
  63. return createGradient(gradient, coords, baseTransform, transform);
  64. }
  65. private P createGradient(MultipleGradientPaint gradient, List<Double> coords,
  66. AffineTransform baseTransform, AffineTransform transform) {
  67. List<Double> matrix = createTransform(gradient, baseTransform, transform);
  68. List<Color> colors = createColors(gradient);
  69. List<Double> bounds = createBounds(gradient);
  70. //Gradients are currently restricted to sRGB
  71. PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  72. return makeGradient(gradient instanceof RadialGradientPaint, colSpace, colors, bounds, coords, matrix);
  73. }
  74. private List<Double> createTransform(MultipleGradientPaint gradient,
  75. AffineTransform baseTransform, AffineTransform transform) {
  76. AffineTransform gradientTransform = new AffineTransform(baseTransform);
  77. gradientTransform.concatenate(transform);
  78. gradientTransform.concatenate(gradient.getTransform());
  79. List<Double> matrix = new ArrayList<Double>(6);
  80. double[] m = new double[6];
  81. gradientTransform.getMatrix(m);
  82. for (double d : m) {
  83. matrix.add(Double.valueOf(d));
  84. }
  85. return matrix;
  86. }
  87. private List<Color> createColors(MultipleGradientPaint gradient) {
  88. Color[] svgColors = gradient.getColors();
  89. List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
  90. float[] fractions = gradient.getFractions();
  91. if (fractions[0] > 0f) {
  92. gradientColors.add(svgColors[0]);
  93. }
  94. for (Color c : svgColors) {
  95. gradientColors.add(c);
  96. }
  97. if (fractions[fractions.length - 1] < 1f) {
  98. gradientColors.add(svgColors[svgColors.length - 1]);
  99. }
  100. return gradientColors;
  101. }
  102. private List<Double> createBounds(MultipleGradientPaint gradient) {
  103. // TODO is the conversion to double necessary?
  104. float[] fractions = gradient.getFractions();
  105. List<Double> bounds = new java.util.ArrayList<Double>(fractions.length);
  106. for (float offset : fractions) {
  107. if (0f < offset && offset < 1f) {
  108. bounds.add(Double.valueOf(offset));
  109. }
  110. }
  111. return bounds;
  112. }
  113. /**
  114. * Creates a new gradient.
  115. *
  116. * @param radial whether the gradient is linear or radial
  117. * @param colorspace the colorspace used in PDF and Postscript
  118. * @param colors the colors to be used in the gradient
  119. * @param bounds the bounds of each color
  120. * @param coords the coordinates of the gradient
  121. * @param matrix the transformation matrix
  122. * @return the Pattern object of the gradient
  123. */
  124. protected P makeGradient(boolean radial, PDFDeviceColorSpace colorspace,
  125. List<Color> colors, List<Double> bounds, List<Double> coords, List<Double> matrix) {
  126. List<Function> functions = new ArrayList<Function>();
  127. // if 5 elements, the penultimate element is 3.
  128. // do not go beyond that, because you always need
  129. // to have a next color when creating the function.
  130. for (int currentPosition = 0, lastPosition = colors.size() - 1; currentPosition < lastPosition;
  131. currentPosition++) { // for every consecutive color pair
  132. Color currentColor = colors.get(currentPosition);
  133. Color nextColor = colors.get(currentPosition + 1);
  134. // colorspace must be consistent, so we simply convert to sRGB where necessary
  135. if (!currentColor.getColorSpace().isCS_sRGB()) {
  136. //Convert to sRGB
  137. currentColor = ColorUtil.toSRGBColor(currentColor);
  138. colors.set(currentPosition, currentColor);
  139. }
  140. if (!nextColor.getColorSpace().isCS_sRGB()) {
  141. //Convert to sRGB
  142. nextColor = ColorUtil.toSRGBColor(nextColor);
  143. colors.set(currentPosition + 1, nextColor);
  144. }
  145. List<Double> c0 = toColorVector(currentColor);
  146. List<Double> c1 = toColorVector(nextColor);
  147. Function function = makeFunction(2, null, null, c0, c1, 1.0);
  148. functions.add(function);
  149. }
  150. Function function = makeFunction(3, null, null, functions, bounds, null);
  151. Shading shading;
  152. if (radial) {
  153. if (coords.size() == 6) {
  154. shading = makeShading(3, colorspace, null, null, false, coords, null, function, null);
  155. } else { // if the center x, center y, and radius specifiy
  156. // the gradient, then assume the same center x, center y,
  157. // and radius of zero for the other necessary component
  158. List<Double> newCoords = new ArrayList<Double>();
  159. newCoords.add(coords.get(0));
  160. newCoords.add(coords.get(1));
  161. newCoords.add(coords.get(2));
  162. newCoords.add(coords.get(0));
  163. newCoords.add(coords.get(1));
  164. newCoords.add(Double.valueOf(0.0));
  165. shading = makeShading(3, colorspace, null, null, false, newCoords,
  166. null, function, null);
  167. }
  168. } else {
  169. shading = makeShading(2, colorspace, null, null, false, coords,
  170. null, function, null);
  171. }
  172. return makePattern(2, shading, null, null, matrix);
  173. }
  174. public abstract Function makeFunction(int functionType, List<Double> theDomain,
  175. List<Double> theRange, List<Function> theFunctions,
  176. List<Double> theBounds, List<Double> theEncode);
  177. public abstract Function makeFunction(int functionType, List<Double> theDomain,
  178. List<Double> theRange, List<Double> theCZero, List<Double> theCOne,
  179. double theInterpolationExponentN);
  180. public abstract Shading makeShading(int theShadingType,
  181. PDFDeviceColorSpace theColorSpace, List<Double> theBackground, List<Double> theBBox,
  182. boolean theAntiAlias, List<Double> theCoords, List<Double> theDomain,
  183. Function theFunction, List<Integer> theExtend);
  184. public abstract P makePattern(int thePatternType, Shading theShading, List theXUID,
  185. StringBuffer theExtGState, List<Double> theMatrix);
  186. private List<Double> toColorVector(Color nextColor) {
  187. List<Double> vector = new java.util.ArrayList<Double>();
  188. float[] comps = nextColor.getColorComponents(null);
  189. for (int i = 0, c = comps.length; i < c; i++) {
  190. vector.add(Double.valueOf(comps[i]));
  191. }
  192. return vector;
  193. }
  194. }