選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GradientMaker.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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(startPoint.getX());
  40. coords.add(startPoint.getY());
  41. coords.add(endPoint.getX());
  42. coords.add(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 focal 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(center.getX() + dx);
  62. coords.add(center.getY() + dy);
  63. coords.add(0d);
  64. coords.add(center.getX());
  65. coords.add(center.getY());
  66. coords.add(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(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 ArrayList<Float>(fractions.length);
  102. for (float offset : fractions) {
  103. if (0f < offset) {
  104. bounds.add(offset);
  105. }
  106. }
  107. float last = bounds.get(bounds.size() - 1);
  108. if (last == 1f) {
  109. bounds.remove(bounds.size() - 1);
  110. }
  111. return bounds;
  112. }
  113. private static List<Function> makeFunctions(MultipleGradientPaint gradient) {
  114. List<Color> colors = makeColors(gradient);
  115. List<Function> functions = new ArrayList<Function>();
  116. for (int currentPosition = 0, lastPosition = colors.size() - 1;
  117. currentPosition < lastPosition;
  118. currentPosition++) {
  119. Color currentColor = colors.get(currentPosition);
  120. Color nextColor = colors.get(currentPosition + 1);
  121. float[] c0 = currentColor.getColorComponents(null);
  122. float[] c1 = nextColor.getColorComponents(null);
  123. Function function = new Function(null, null, c0, c1, 1.0);
  124. functions.add(function);
  125. }
  126. return functions;
  127. }
  128. private static List<Color> makeColors(MultipleGradientPaint gradient) {
  129. Color[] svgColors = gradient.getColors();
  130. List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
  131. float[] fractions = gradient.getFractions();
  132. if (fractions[0] > 0f) {
  133. gradientColors.add(getsRGBColor(svgColors[0]));
  134. }
  135. for (Color c : svgColors) {
  136. gradientColors.add(getsRGBColor(c));
  137. }
  138. if (fractions[fractions.length - 1] < 1f) {
  139. gradientColors.add(getsRGBColor(svgColors[svgColors.length - 1]));
  140. }
  141. return gradientColors;
  142. }
  143. static void outputDoubles(StringBuilder out, DoubleFormatter doubleFormatter,
  144. List<? extends Number> numbers) {
  145. out.append("[ ");
  146. for (Number n : numbers) {
  147. out.append(doubleFormatter.formatDouble(n.doubleValue()));
  148. out.append(" ");
  149. }
  150. out.append("]");
  151. }
  152. }