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.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 GradientMaker<P extends Pattern> {
  30. public P makeLinearGradient(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 makeGradient(gp, coords, baseTransform, transform);
  40. }
  41. public P makeRadialGradient(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>(6);
  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 makeGradient(gradient, coords, baseTransform, transform);
  64. }
  65. private P makeGradient(MultipleGradientPaint gradient, List<Double> coords,
  66. AffineTransform baseTransform, AffineTransform transform) {
  67. List<Double> matrix = makeTransform(gradient, baseTransform, transform);
  68. List<Double> bounds = makeBounds(gradient);
  69. List<Function> functions = makeFunctions(gradient);
  70. // Gradients are currently restricted to sRGB
  71. PDFDeviceColorSpace colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  72. Function function = new Function(3, null, null, functions, bounds, null);
  73. int shadingType = gradient instanceof LinearGradientPaint ? 2 : 3;
  74. Shading shading = new Shading(shadingType, colorSpace, coords, function);
  75. return makePattern(2, shading, matrix);
  76. }
  77. private List<Double> makeTransform(MultipleGradientPaint gradient,
  78. AffineTransform baseTransform, AffineTransform transform) {
  79. AffineTransform gradientTransform = new AffineTransform(baseTransform);
  80. gradientTransform.concatenate(transform);
  81. gradientTransform.concatenate(gradient.getTransform());
  82. List<Double> matrix = new ArrayList<Double>(6);
  83. double[] m = new double[6];
  84. gradientTransform.getMatrix(m);
  85. for (double d : m) {
  86. matrix.add(Double.valueOf(d));
  87. }
  88. return matrix;
  89. }
  90. private Color getsRGBColor(Color c) {
  91. // Color space must be consistent, so convert to sRGB if necessary
  92. // TODO really?
  93. return c.getColorSpace().isCS_sRGB() ? c : ColorUtil.toSRGBColor(c);
  94. }
  95. private List<Double> makeBounds(MultipleGradientPaint gradient) {
  96. // TODO is the conversion to double necessary?
  97. float[] fractions = gradient.getFractions();
  98. List<Double> bounds = new java.util.ArrayList<Double>(fractions.length);
  99. for (float offset : fractions) {
  100. if (0f < offset && offset < 1f) {
  101. bounds.add(Double.valueOf(offset));
  102. }
  103. }
  104. return bounds;
  105. }
  106. private List<Function> makeFunctions(MultipleGradientPaint gradient) {
  107. List<Color> colors = makeColors(gradient);
  108. List<Function> functions = new ArrayList<Function>();
  109. for (int currentPosition = 0, lastPosition = colors.size() - 1;
  110. currentPosition < lastPosition;
  111. currentPosition++) {
  112. Color currentColor = colors.get(currentPosition);
  113. Color nextColor = colors.get(currentPosition + 1);
  114. float[] c0 = currentColor.getColorComponents(null);
  115. float[] c1 = nextColor.getColorComponents(null);
  116. Function function = new Function(2, null, null, c0, c1, 1.0);
  117. functions.add(function);
  118. }
  119. return functions;
  120. }
  121. private List<Color> makeColors(MultipleGradientPaint gradient) {
  122. Color[] svgColors = gradient.getColors();
  123. List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
  124. float[] fractions = gradient.getFractions();
  125. if (fractions[0] > 0f) {
  126. gradientColors.add(getsRGBColor(svgColors[0]));
  127. }
  128. for (Color c : svgColors) {
  129. gradientColors.add(getsRGBColor(c));
  130. }
  131. if (fractions[fractions.length - 1] < 1f) {
  132. gradientColors.add(getsRGBColor(svgColors[svgColors.length - 1]));
  133. }
  134. return gradientColors;
  135. }
  136. protected abstract P makePattern(int patternType, Shading shading, List<Double> matrix);
  137. }