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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Graphics;
  20. import java.awt.Paint;
  21. import java.awt.geom.AffineTransform;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.batik.ext.awt.LinearGradientPaint;
  28. import org.apache.batik.ext.awt.RadialGradientPaint;
  29. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  30. import org.apache.xmlgraphics.ps.PSGenerator;
  31. import org.apache.fop.render.gradient.Function;
  32. import org.apache.fop.render.gradient.GradientMaker;
  33. import org.apache.fop.render.gradient.Pattern;
  34. import org.apache.fop.render.gradient.Shading;
  35. public class PSSVGGraphics2D extends PSGraphics2D {
  36. private static final Log LOG = LogFactory.getLog(PSSVGGraphics2D.class);
  37. /**
  38. * Create a new Graphics2D that generates PostScript code.
  39. * @param textAsShapes True if text should be rendered as graphics
  40. * @see org.apache.xmlgraphics.java2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
  41. */
  42. public PSSVGGraphics2D(boolean textAsShapes) {
  43. super(textAsShapes);
  44. }
  45. /**
  46. * Create a new Graphics2D that generates PostScript code.
  47. * @param textAsShapes True if text should be rendered as graphics
  48. * @param gen PostScript generator to use for output
  49. * @see org.apache.xmlgraphics.java2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
  50. */
  51. public PSSVGGraphics2D(boolean textAsShapes, PSGenerator gen) {
  52. super(textAsShapes, gen);
  53. }
  54. /**
  55. * Constructor for creating copies
  56. * @param g parent PostScript Graphics2D
  57. */
  58. public PSSVGGraphics2D(PSGraphics2D g) {
  59. super(g);
  60. }
  61. protected void applyPaint(Paint paint, boolean fill) {
  62. super.applyPaint(paint, fill);
  63. if (paint instanceof LinearGradientPaint) {
  64. Pattern pattern = GradientMaker.makeLinearGradient((LinearGradientPaint) paint,
  65. getBaseTransform(), getTransform());
  66. try {
  67. gen.write(toString(pattern));
  68. } catch (IOException ioe) {
  69. handleIOException(ioe);
  70. }
  71. } else if (paint instanceof RadialGradientPaint) {
  72. Pattern pattern = GradientMaker.makeRadialGradient((RadialGradientPaint) paint,
  73. getBaseTransform(), getTransform());
  74. try {
  75. gen.write(toString(pattern));
  76. } catch (IOException ioe) {
  77. handleIOException(ioe);
  78. }
  79. }
  80. }
  81. /**
  82. * Outputs the radial or axial pattern as a string dictionary to insert
  83. * into a postscript document.
  84. * @param pattern
  85. */
  86. private String toString(Pattern pattern) {
  87. StringBuilder p = new StringBuilder(64);
  88. p.append("/Pattern setcolorspace\n");
  89. p.append("<< \n/Type /Pattern \n");
  90. p.append("/PatternType " + pattern.getPatternType() + " \n");
  91. if (pattern.getShading() != null) {
  92. p.append("/Shading ");
  93. outputShading(p, pattern.getShading());
  94. p.append(" \n");
  95. }
  96. p.append(">> \n");
  97. p.append("[ ");
  98. for (double m : pattern.getMatrix()) {
  99. p.append(Double.toString(m)); // TODO refactor so that PSGenerator.formatDouble can be used
  100. p.append(" ");
  101. }
  102. p.append("] ");
  103. p.append("makepattern setcolor\n");
  104. return p.toString();
  105. }
  106. private void outputShading(StringBuilder p, Shading shading) {
  107. final Function function = shading.getFunction();
  108. Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() {
  109. public void outputFunction(StringBuilder out) {
  110. List<String> functionsStrings = new ArrayList<String>(function.getFunctions().size());
  111. for (Function f : function.getFunctions()) {
  112. functionsStrings.add(functionToString(f));
  113. }
  114. out.append(function.toWriteableString(functionsStrings));
  115. }
  116. };
  117. shading.output(p, functionRenderer);
  118. }
  119. private String functionToString(Function function) {
  120. List<String> functionsStrings = new ArrayList<String>(function.getFunctions().size());
  121. for (Function f : function.getFunctions()) {
  122. functionsStrings.add(functionToString(f));
  123. }
  124. return function.toWriteableString(functionsStrings);
  125. }
  126. protected AffineTransform getBaseTransform() {
  127. AffineTransform at = new AffineTransform(this.getTransform());
  128. return at;
  129. }
  130. /**
  131. * Creates a new <code>Graphics</code> object that is
  132. * a copy of this <code>Graphics</code> object.
  133. * @return a new graphics context that is a copy of
  134. * this graphics context.
  135. */
  136. @Override
  137. public Graphics create() {
  138. preparePainting();
  139. return new PSSVGGraphics2D(this);
  140. }
  141. }