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.

PSSVGGraphics2D.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.ps.svg;
  19. import java.awt.Color;
  20. import java.awt.Graphics;
  21. import java.awt.Paint;
  22. import java.awt.geom.AffineTransform;
  23. import java.awt.geom.Point2D;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.batik.ext.awt.LinearGradientPaint;
  30. import org.apache.batik.ext.awt.MultipleGradientPaint;
  31. import org.apache.batik.ext.awt.RadialGradientPaint;
  32. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  33. import org.apache.xmlgraphics.ps.PSGenerator;
  34. import org.apache.fop.pdf.PDFDeviceColorSpace;
  35. import org.apache.fop.render.shading.Function;
  36. import org.apache.fop.render.shading.GradientRegistrar;
  37. import org.apache.fop.render.shading.PSGradientFactory;
  38. import org.apache.fop.render.shading.Pattern;
  39. import org.apache.fop.render.shading.Shading;
  40. public class PSSVGGraphics2D extends PSGraphics2D implements GradientRegistrar {
  41. private static final Log LOG = LogFactory.getLog(PSSVGGraphics2D.class);
  42. /**
  43. * Create a new Graphics2D that generates PostScript code.
  44. * @param textAsShapes True if text should be rendered as graphics
  45. * @see org.apache.xmlgraphics.java2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
  46. */
  47. public PSSVGGraphics2D(boolean textAsShapes) {
  48. super(textAsShapes);
  49. }
  50. /**
  51. * Create a new Graphics2D that generates PostScript code.
  52. * @param textAsShapes True if text should be rendered as graphics
  53. * @param gen PostScript generator to use for output
  54. * @see org.apache.xmlgraphics.java2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
  55. */
  56. public PSSVGGraphics2D(boolean textAsShapes, PSGenerator gen) {
  57. super(textAsShapes, gen);
  58. }
  59. /**
  60. * Constructor for creating copies
  61. * @param g parent PostScript Graphics2D
  62. */
  63. public PSSVGGraphics2D(PSGraphics2D g) {
  64. super(g);
  65. }
  66. protected void applyPaint(Paint paint, boolean fill) {
  67. super.applyPaint(paint, fill);
  68. if (paint instanceof RadialGradientPaint) {
  69. RadialGradientPaint rgp = (RadialGradientPaint)paint;
  70. try {
  71. handleRadialGradient(rgp, gen);
  72. } catch (IOException ioe) {
  73. handleIOException(ioe);
  74. }
  75. } else if (paint instanceof LinearGradientPaint) {
  76. LinearGradientPaint lgp = (LinearGradientPaint)paint;
  77. try {
  78. handleLinearGradient(lgp, gen);
  79. } catch (IOException ioe) {
  80. handleIOException(ioe);
  81. }
  82. }
  83. }
  84. private void handleLinearGradient(LinearGradientPaint gp, PSGenerator gen) throws IOException {
  85. MultipleGradientPaint.CycleMethodEnum cycle = gp.getCycleMethod();
  86. if (cycle != MultipleGradientPaint.NO_CYCLE) {
  87. return;
  88. }
  89. List<Double> matrix = createGradientTransform(gp);
  90. List<Double> theCoords = new java.util.ArrayList<Double>();
  91. theCoords.add(gp.getStartPoint().getX());
  92. theCoords.add(gp.getStartPoint().getX());
  93. theCoords.add(gp.getEndPoint().getX());
  94. theCoords.add(gp.getEndPoint().getY());
  95. List<Color> colors = createGradientColors(gp);
  96. List<Double> bounds = createGradientBounds(gp);
  97. PDFDeviceColorSpace colSpace;
  98. colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  99. PSGradientFactory gradientFactory = new PSGradientFactory();
  100. PSPattern myPattern = gradientFactory.createGradient(false, colSpace,
  101. colors, bounds, theCoords, matrix);
  102. gen.write(myPattern.toString());
  103. }
  104. private void handleRadialGradient(RadialGradientPaint gp, PSGenerator gen) throws IOException {
  105. MultipleGradientPaint.CycleMethodEnum cycle = gp.getCycleMethod();
  106. if (cycle != MultipleGradientPaint.NO_CYCLE) {
  107. return;
  108. }
  109. List<Double> matrix = createGradientTransform(gp);
  110. double ar = gp.getRadius();
  111. Point2D ac = gp.getCenterPoint();
  112. Point2D af = gp.getFocusPoint();
  113. List<Double> theCoords = new java.util.ArrayList<Double>();
  114. double dx = af.getX() - ac.getX();
  115. double dy = af.getY() - ac.getY();
  116. double d = Math.sqrt(dx * dx + dy * dy);
  117. if (d > ar) {
  118. // the center point af must be within the circle with
  119. // radius ar centered at ac so limit it to that.
  120. double scale = (ar * .9999) / d;
  121. dx = dx * scale;
  122. dy = dy * scale;
  123. }
  124. theCoords.add(new Double(ac.getX() + dx)); // Fx
  125. theCoords.add(new Double(ac.getY() + dy)); // Fy
  126. theCoords.add(new Double(0));
  127. theCoords.add(new Double(ac.getX()));
  128. theCoords.add(new Double(ac.getY()));
  129. theCoords.add(new Double(ar));
  130. List<Color> colors = createGradientColors(gp);
  131. List<Double> bounds = createGradientBounds(gp);
  132. PDFDeviceColorSpace colSpace;
  133. colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  134. PSGradientFactory gradientFactory = new PSGradientFactory();
  135. PSPattern myPattern = gradientFactory.createGradient(true, colSpace,
  136. colors, bounds, theCoords, matrix);
  137. gen.write(myPattern.toString());
  138. }
  139. private List<Double> createGradientTransform(MultipleGradientPaint gradient) {
  140. AffineTransform transform = new AffineTransform(getBaseTransform());
  141. transform.concatenate(getTransform());
  142. transform.concatenate(gradient.getTransform());
  143. List<Double> matrix = new ArrayList<Double>(6);
  144. double[] m = new double[6];
  145. transform.getMatrix(m);
  146. for (double d : m) {
  147. matrix.add(Double.valueOf(d));
  148. }
  149. return matrix;
  150. }
  151. private List<Color> createGradientColors(MultipleGradientPaint gradient) {
  152. Color[] svgColors = gradient.getColors();
  153. List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
  154. float[] fractions = gradient.getFractions();
  155. if (fractions[0] > 0f) {
  156. gradientColors.add(svgColors[0]);
  157. }
  158. for (Color c : svgColors) {
  159. gradientColors.add(c);
  160. }
  161. if (fractions[fractions.length - 1] < 1f) {
  162. gradientColors.add(svgColors[svgColors.length - 1]);
  163. }
  164. return gradientColors;
  165. }
  166. private List<Double> createGradientBounds(MultipleGradientPaint gradient) {
  167. // TODO is the conversion to double necessary?
  168. float[] fractions = gradient.getFractions();
  169. List<Double> bounds = new java.util.ArrayList<Double>(fractions.length);
  170. for (float offset : fractions) {
  171. if (0f < offset && offset < 1f) {
  172. bounds.add(Double.valueOf(offset));
  173. }
  174. }
  175. return bounds;
  176. }
  177. protected AffineTransform getBaseTransform() {
  178. AffineTransform at = new AffineTransform(this.getTransform());
  179. return at;
  180. }
  181. /**
  182. * Creates a new <code>Graphics</code> object that is
  183. * a copy of this <code>Graphics</code> object.
  184. * @return a new graphics context that is a copy of
  185. * this graphics context.
  186. */
  187. @Override
  188. public Graphics create() {
  189. preparePainting();
  190. return new PSSVGGraphics2D(this);
  191. }
  192. /**
  193. * Registers a function object against the output format document
  194. * @param function The function object to register
  195. * @return Returns either the function which has already been registered
  196. * or the current new registered object.
  197. */
  198. public Function registerFunction(Function function) {
  199. //Objects aren't needed to be registered in Postscript
  200. return function;
  201. }
  202. /**
  203. * Registers a shading object against the otuput format document
  204. * @param shading The shading object to register
  205. * @return Returs either the shading which has already been registered
  206. * or the current new registered object
  207. */
  208. public Shading registerShading(Shading shading) {
  209. //Objects aren't needed to be registered in Postscript
  210. return shading;
  211. }
  212. /**
  213. * Registers a pattern object against the output format document
  214. * @param pattern The pattern object to register
  215. * @return Returns either the pattern which has already been registered
  216. * or the current new registered object
  217. */
  218. public Pattern registerPattern(Pattern pattern) {
  219. // TODO Auto-generated method stub
  220. return pattern;
  221. }
  222. }