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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.batik.ext.awt.LinearGradientPaint;
  29. import org.apache.batik.ext.awt.MultipleGradientPaint;
  30. import org.apache.batik.ext.awt.RadialGradientPaint;
  31. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  32. import org.apache.xmlgraphics.ps.PSGenerator;
  33. import org.apache.fop.pdf.PDFDeviceColorSpace;
  34. import org.apache.fop.render.shading.Function;
  35. import org.apache.fop.render.shading.GradientFactory;
  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 lgp, PSGenerator gen) throws IOException {
  85. MultipleGradientPaint.CycleMethodEnum cycle = lgp.getCycleMethod();
  86. if (cycle != MultipleGradientPaint.NO_CYCLE) {
  87. return;
  88. }
  89. float[] fractions = lgp.getFractions();
  90. Color[] cols = lgp.getColors();
  91. AffineTransform transform = new AffineTransform(getBaseTransform());
  92. transform.concatenate(getTransform());
  93. transform.concatenate(lgp.getTransform());
  94. List theMatrix = new ArrayList();
  95. double [] mat = new double[6];
  96. transform.getMatrix(mat);
  97. for (int idx = 0; idx < mat.length; idx++) {
  98. theMatrix.add(Double.valueOf(mat[idx]));
  99. }
  100. List<Double> theCoords = new java.util.ArrayList<Double>();
  101. AffineTransform start = applyTransform(lgp.getTransform(),
  102. lgp.getStartPoint().getX(), lgp.getStartPoint().getY());
  103. AffineTransform end = applyTransform(lgp.getTransform(), lgp.getEndPoint().getX(), lgp.getEndPoint().getY());
  104. double startX = start.getTranslateX();
  105. double startY = start.getTranslateY();
  106. double endX = end.getTranslateX();
  107. double endY = end.getTranslateY();
  108. double width = endX - startX;
  109. double height = endY - startY;
  110. startX = startX + width * fractions[0];
  111. endX = endX - width * (1 - fractions[fractions.length - 1]);
  112. startY = startY + (height * fractions[0]);
  113. endY = endY - height * (1 - fractions[fractions.length - 1]);
  114. theCoords.add(startX);
  115. theCoords.add(startY);
  116. theCoords.add(endX);
  117. theCoords.add(endY);
  118. List<Color> someColors = new java.util.ArrayList<Color>();
  119. for (int count = 0; count < cols.length; count++) {
  120. Color c1 = cols[count];
  121. if (c1.getAlpha() != 255) {
  122. LOG.warn("Opacity is not currently supported for Postscript output");
  123. }
  124. someColors.add(c1);
  125. }
  126. List<Double> theBounds = new java.util.ArrayList<Double>();
  127. for (int count = 1; count < fractions.length - 1; count++) {
  128. float offset = fractions[count];
  129. theBounds.add(Double.valueOf(offset));
  130. }
  131. PDFDeviceColorSpace colSpace;
  132. colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  133. PSGradientFactory gradientFactory = (PSGradientFactory)GradientFactory.newInstance(this);
  134. PSPattern myPattern = gradientFactory.createGradient(false, colSpace,
  135. someColors, theBounds, theCoords, theMatrix);
  136. gen.write(myPattern.toString());
  137. }
  138. private void handleRadialGradient(RadialGradientPaint rgp, PSGenerator gen) throws IOException {
  139. MultipleGradientPaint.CycleMethodEnum cycle = rgp.getCycleMethod();
  140. if (cycle != MultipleGradientPaint.NO_CYCLE) {
  141. return;
  142. }
  143. AffineTransform transform;
  144. transform = new AffineTransform(getBaseTransform());
  145. transform.concatenate(getTransform());
  146. transform.concatenate(rgp.getTransform());
  147. AffineTransform resultCentre = applyTransform(rgp.getTransform(),
  148. rgp.getCenterPoint().getX(), rgp.getCenterPoint().getY());
  149. AffineTransform resultFocus = applyTransform(rgp.getTransform(),
  150. rgp.getFocusPoint().getX(), rgp.getFocusPoint().getY());
  151. double scale = Math.sqrt(rgp.getTransform().getDeterminant());
  152. double radius = rgp.getRadius() * scale;
  153. double centreX = resultCentre.getTranslateX();
  154. double centreY = resultCentre.getTranslateY();
  155. double focusX = resultFocus.getTranslateX();
  156. double focusY = resultFocus.getTranslateY();
  157. List<Double> theMatrix = new java.util.ArrayList<Double>();
  158. double [] mat = new double[6];
  159. transform.getMatrix(mat);
  160. for (int idx = 0; idx < mat.length; idx++) {
  161. theMatrix.add(Double.valueOf(mat[idx]));
  162. }
  163. List<Double> theCoords = new java.util.ArrayList<Double>();
  164. float[] fractions = rgp.getFractions();
  165. theCoords.add(centreX);
  166. theCoords.add(centreY);
  167. theCoords.add(radius * rgp.getFractions()[0]);
  168. theCoords.add(focusX);
  169. theCoords.add(focusY);
  170. theCoords.add(radius * fractions[fractions.length - 1]);
  171. Color[] cols = rgp.getColors();
  172. List<Color> someColors = new java.util.ArrayList<Color>();
  173. for (int count = 0; count < cols.length; count++) {
  174. Color cc = cols[count];
  175. if (cc.getAlpha() != 255) {
  176. /* This should never happen because radial gradients with opacity should now
  177. * be rasterized in the PSImageHandlerSVG class. Please see the shouldRaster()
  178. * method for more information. */
  179. LOG.warn("Opacity is not currently supported for Postscript output");
  180. }
  181. someColors.add(cc);
  182. }
  183. List<Double> theBounds = new java.util.ArrayList<Double>();
  184. for (int count = 1; count < fractions.length - 1; count++) {
  185. float offset = fractions[count];
  186. theBounds.add(Double.valueOf(offset));
  187. }
  188. PDFDeviceColorSpace colSpace;
  189. colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  190. PSGradientFactory gradientFactory = (PSGradientFactory)GradientFactory.newInstance(this);
  191. PSPattern myPattern = gradientFactory.createGradient(true, colSpace,
  192. someColors, theBounds, theCoords, theMatrix);
  193. gen.write(myPattern.toString());
  194. }
  195. private AffineTransform applyTransform(AffineTransform base, double posX, double posY) {
  196. AffineTransform result = AffineTransform.getTranslateInstance(posX, posY);
  197. AffineTransform orig = base;
  198. orig.concatenate(result);
  199. return orig;
  200. }
  201. protected AffineTransform getBaseTransform() {
  202. AffineTransform at = new AffineTransform(this.getTransform());
  203. return at;
  204. }
  205. /**
  206. * Creates a new <code>Graphics</code> object that is
  207. * a copy of this <code>Graphics</code> object.
  208. * @return a new graphics context that is a copy of
  209. * this graphics context.
  210. */
  211. @Override
  212. public Graphics create() {
  213. preparePainting();
  214. return new PSSVGGraphics2D(this);
  215. }
  216. /**
  217. * Registers a function object against the output format document
  218. * @param function The function object to register
  219. * @return Returns either the function which has already been registered
  220. * or the current new registered object.
  221. */
  222. public Function registerFunction(Function function) {
  223. //Objects aren't needed to be registered in Postscript
  224. return function;
  225. }
  226. /**
  227. * Registers a shading object against the otuput format document
  228. * @param shading The shading object to register
  229. * @return Returs either the shading which has already been registered
  230. * or the current new registered object
  231. */
  232. public Shading registerShading(Shading shading) {
  233. //Objects aren't needed to be registered in Postscript
  234. return shading;
  235. }
  236. /**
  237. * Registers a pattern object against the output format document
  238. * @param pattern The pattern object to register
  239. * @return Returns either the pattern which has already been registered
  240. * or the current new registered object
  241. */
  242. public Pattern registerPattern(Pattern pattern) {
  243. // TODO Auto-generated method stub
  244. return pattern;
  245. }
  246. }