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.

GradientMaker.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.gradient;
  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 final class GradientMaker {
  30. public interface DoubleFormatter {
  31. String formatDouble(double d);
  32. }
  33. private GradientMaker() { }
  34. public static Pattern makeLinearGradient(LinearGradientPaint gp,
  35. AffineTransform baseTransform, AffineTransform transform) {
  36. Point2D startPoint = gp.getStartPoint();
  37. Point2D endPoint = gp.getEndPoint();
  38. List<Double> coords = new java.util.ArrayList<Double>(4);
  39. coords.add(Double.valueOf(startPoint.getX()));
  40. coords.add(Double.valueOf(startPoint.getY()));
  41. coords.add(Double.valueOf(endPoint.getX()));
  42. coords.add(Double.valueOf(endPoint.getY()));
  43. return makeGradient(gp, coords, baseTransform, transform);
  44. }
  45. public static Pattern makeRadialGradient(RadialGradientPaint gradient,
  46. AffineTransform baseTransform, AffineTransform transform) {
  47. double radius = gradient.getRadius();
  48. Point2D center = gradient.getCenterPoint();
  49. Point2D focus = gradient.getFocusPoint();
  50. double dx = focus.getX() - center.getX();
  51. double dy = focus.getY() - center.getY();
  52. double d = Math.sqrt(dx * dx + dy * dy);
  53. if (d > radius) {
  54. // The center point must be within the circle with
  55. // radius radius centered at center so limit it to that.
  56. double scale = (radius * .9999) / d;
  57. dx *= scale;
  58. dy *= scale;
  59. }
  60. List<Double> coords = new java.util.ArrayList<Double>(6);
  61. coords.add(Double.valueOf(center.getX() + dx));
  62. coords.add(Double.valueOf(center.getY() + dy));
  63. coords.add(Double.valueOf(0));
  64. coords.add(Double.valueOf(center.getX()));
  65. coords.add(Double.valueOf(center.getY()));
  66. coords.add(Double.valueOf(radius));
  67. return makeGradient(gradient, coords, baseTransform, transform);
  68. }
  69. private static Pattern makeGradient(MultipleGradientPaint gradient, List<Double> coords,
  70. AffineTransform baseTransform, AffineTransform transform) {
  71. List<Double> matrix = makeTransform(gradient, baseTransform, transform);
  72. List<Float> bounds = makeBounds(gradient);
  73. List<Function> functions = makeFunctions(gradient);
  74. // Gradients are currently restricted to sRGB
  75. PDFDeviceColorSpace colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  76. Function function = new Function(null, null, functions, bounds, null);
  77. int shadingType = gradient instanceof LinearGradientPaint ? 2 : 3;
  78. Shading shading = new Shading(shadingType, colorSpace, coords, function);
  79. return new Pattern(2, shading, matrix);
  80. }
  81. private static List<Double> makeTransform(MultipleGradientPaint gradient,
  82. AffineTransform baseTransform, AffineTransform transform) {
  83. AffineTransform gradientTransform = new AffineTransform(baseTransform);
  84. gradientTransform.concatenate(transform);
  85. gradientTransform.concatenate(gradient.getTransform());
  86. List<Double> matrix = new ArrayList<Double>(6);
  87. double[] m = new double[6];
  88. gradientTransform.getMatrix(m);
  89. for (double d : m) {
  90. matrix.add(Double.valueOf(d));
  91. }
  92. return matrix;
  93. }
  94. private static Color getsRGBColor(Color c) {
  95. // Color space must be consistent, so convert to sRGB if necessary
  96. // TODO really?
  97. return c.getColorSpace().isCS_sRGB() ? c : ColorUtil.toSRGBColor(c);
  98. }
  99. private static List<Float> makeBounds(MultipleGradientPaint gradient) {
  100. float[] fractions = gradient.getFractions();
  101. List<Float> bounds = new java.util.ArrayList<Float>(fractions.length);
  102. for (float offset : fractions) {
  103. if (0f < offset && offset < 1f) {
  104. bounds.add(offset);
  105. }
  106. }
  107. return bounds;
  108. }
  109. private static List<Function> makeFunctions(MultipleGradientPaint gradient) {
  110. List<Color> colors = makeColors(gradient);
  111. List<Function> functions = new ArrayList<Function>();
  112. for (int currentPosition = 0, lastPosition = colors.size() - 1;
  113. currentPosition < lastPosition;
  114. currentPosition++) {
  115. Color currentColor = colors.get(currentPosition);
  116. Color nextColor = colors.get(currentPosition + 1);
  117. float[] c0 = currentColor.getColorComponents(null);
  118. float[] c1 = nextColor.getColorComponents(null);
  119. Function function = new Function(null, null, c0, c1, 1.0);
  120. functions.add(function);
  121. }
  122. return functions;
  123. }
  124. private static List<Color> makeColors(MultipleGradientPaint gradient) {
  125. Color[] svgColors = gradient.getColors();
  126. List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
  127. float[] fractions = gradient.getFractions();
  128. if (fractions[0] > 0f) {
  129. gradientColors.add(getsRGBColor(svgColors[0]));
  130. }
  131. for (Color c : svgColors) {
  132. gradientColors.add(getsRGBColor(c));
  133. }
  134. if (fractions[fractions.length - 1] < 1f) {
  135. gradientColors.add(getsRGBColor(svgColors[svgColors.length - 1]));
  136. }
  137. return gradientColors;
  138. }
  139. static void outputDoubles(StringBuilder out, DoubleFormatter doubleFormatter,
  140. List<? extends Number> numbers) {
  141. out.append("[ ");
  142. for (Number n : numbers) {
  143. out.append(doubleFormatter.formatDouble(n.doubleValue()));
  144. out.append(" ");
  145. }
  146. out.append("]");
  147. }
  148. }