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.4KB

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