Moved PDF/PSGradientMaker into dedicated packages Factory stands for a pattern relating to application deployment. This is just about rendering a gradient to a certain output format. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1609758 13f79535-47bb-0310-9956-ffa450edef68tags/fop-2_0
* limitations under the License. | * limitations under the License. | ||||
*/ | */ | ||||
package org.apache.fop.render.shading; | |||||
package org.apache.fop.render.pdf.svg; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import org.apache.fop.pdf.PDFFunction; | import org.apache.fop.pdf.PDFFunction; | ||||
import org.apache.fop.pdf.PDFPattern; | import org.apache.fop.pdf.PDFPattern; | ||||
import org.apache.fop.pdf.PDFShading; | import org.apache.fop.pdf.PDFShading; | ||||
import org.apache.fop.render.shading.Function; | |||||
import org.apache.fop.render.shading.GradientMaker; | |||||
import org.apache.fop.render.shading.Shading; | |||||
import org.apache.fop.svg.PDFGraphics2D; | import org.apache.fop.svg.PDFGraphics2D; | ||||
public class PDFGradientFactory extends GradientFactory<PDFPattern> { | |||||
public class PDFGradientMaker extends GradientMaker<PDFPattern> { | |||||
private final PDFGraphics2D graphics2D; | private final PDFGraphics2D graphics2D; | ||||
public PDFGradientFactory(PDFGraphics2D pdfGraphics2D) { | |||||
public PDFGradientMaker(PDFGraphics2D pdfGraphics2D) { | |||||
this.graphics2D = pdfGraphics2D; | this.graphics2D = pdfGraphics2D; | ||||
} | } | ||||
* limitations under the License. | * limitations under the License. | ||||
*/ | */ | ||||
package org.apache.fop.render.shading; | |||||
package org.apache.fop.render.ps.svg; | |||||
import java.util.List; | import java.util.List; | ||||
import org.apache.fop.pdf.PDFDeviceColorSpace; | import org.apache.fop.pdf.PDFDeviceColorSpace; | ||||
import org.apache.fop.render.ps.svg.PSPattern; | |||||
import org.apache.fop.render.ps.svg.PSShading; | |||||
import org.apache.fop.render.shading.Function; | |||||
import org.apache.fop.render.shading.GradientMaker; | |||||
import org.apache.fop.render.shading.Shading; | |||||
public class PSGradientFactory extends GradientFactory<PSPattern> { | |||||
public class PSGradientMaker extends GradientMaker<PSPattern> { | |||||
@Override | @Override | ||||
protected Shading makeShading(int shadingType, | protected Shading makeShading(int shadingType, |
import org.apache.xmlgraphics.java2d.ps.PSGraphics2D; | import org.apache.xmlgraphics.java2d.ps.PSGraphics2D; | ||||
import org.apache.xmlgraphics.ps.PSGenerator; | import org.apache.xmlgraphics.ps.PSGenerator; | ||||
import org.apache.fop.render.shading.PSGradientFactory; | |||||
import org.apache.fop.render.shading.Pattern; | import org.apache.fop.render.shading.Pattern; | ||||
protected void applyPaint(Paint paint, boolean fill) { | protected void applyPaint(Paint paint, boolean fill) { | ||||
super.applyPaint(paint, fill); | super.applyPaint(paint, fill); | ||||
if (paint instanceof LinearGradientPaint) { | if (paint instanceof LinearGradientPaint) { | ||||
Pattern pattern = new PSGradientFactory() | |||||
.createLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
Pattern pattern = new PSGradientMaker() | |||||
.makeLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
try { | try { | ||||
gen.write(pattern.toString()); | gen.write(pattern.toString()); | ||||
} catch (IOException ioe) { | } catch (IOException ioe) { | ||||
handleIOException(ioe); | handleIOException(ioe); | ||||
} | } | ||||
} else if (paint instanceof RadialGradientPaint) { | } else if (paint instanceof RadialGradientPaint) { | ||||
Pattern pattern = new PSGradientFactory() | |||||
.createRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
Pattern pattern = new PSGradientMaker() | |||||
.makeRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
try { | try { | ||||
gen.write(pattern.toString()); | gen.write(pattern.toString()); | ||||
} catch (IOException ioe) { | } catch (IOException ioe) { |
import org.apache.fop.pdf.PDFDeviceColorSpace; | import org.apache.fop.pdf.PDFDeviceColorSpace; | ||||
public abstract class GradientFactory<P extends Pattern> { | |||||
public abstract class GradientMaker<P extends Pattern> { | |||||
public P createLinearGradient(LinearGradientPaint gp, | |||||
public P makeLinearGradient(LinearGradientPaint gp, | |||||
AffineTransform baseTransform, AffineTransform transform) { | AffineTransform baseTransform, AffineTransform transform) { | ||||
Point2D startPoint = gp.getStartPoint(); | Point2D startPoint = gp.getStartPoint(); | ||||
Point2D endPoint = gp.getEndPoint(); | Point2D endPoint = gp.getEndPoint(); | ||||
coords.add(new Double(startPoint.getY())); | coords.add(new Double(startPoint.getY())); | ||||
coords.add(new Double(endPoint.getX())); | coords.add(new Double(endPoint.getX())); | ||||
coords.add(new Double(endPoint.getY())); | coords.add(new Double(endPoint.getY())); | ||||
return createGradient(gp, coords, baseTransform, transform); | |||||
return makeGradient(gp, coords, baseTransform, transform); | |||||
} | } | ||||
public P createRadialGradient(RadialGradientPaint gradient, | |||||
public P makeRadialGradient(RadialGradientPaint gradient, | |||||
AffineTransform baseTransform, AffineTransform transform) { | AffineTransform baseTransform, AffineTransform transform) { | ||||
double radius = gradient.getRadius(); | double radius = gradient.getRadius(); | ||||
Point2D center = gradient.getCenterPoint(); | Point2D center = gradient.getCenterPoint(); | ||||
coords.add(Double.valueOf(center.getX())); | coords.add(Double.valueOf(center.getX())); | ||||
coords.add(Double.valueOf(center.getY())); | coords.add(Double.valueOf(center.getY())); | ||||
coords.add(Double.valueOf(radius)); | coords.add(Double.valueOf(radius)); | ||||
return createGradient(gradient, coords, baseTransform, transform); | |||||
return makeGradient(gradient, coords, baseTransform, transform); | |||||
} | } | ||||
private P createGradient(MultipleGradientPaint gradient, List<Double> coords, | |||||
private P makeGradient(MultipleGradientPaint gradient, List<Double> coords, | |||||
AffineTransform baseTransform, AffineTransform transform) { | AffineTransform baseTransform, AffineTransform transform) { | ||||
List<Double> matrix = createTransform(gradient, baseTransform, transform); | |||||
List<Double> bounds = createBounds(gradient); | |||||
List<Function> functions = createFunctions(gradient); | |||||
List<Double> matrix = makeTransform(gradient, baseTransform, transform); | |||||
List<Double> bounds = makeBounds(gradient); | |||||
List<Function> functions = makeFunctions(gradient); | |||||
// Gradients are currently restricted to sRGB | // Gradients are currently restricted to sRGB | ||||
PDFDeviceColorSpace colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB); | PDFDeviceColorSpace colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB); | ||||
Function function = new Function(3, null, null, functions, bounds, null); | Function function = new Function(3, null, null, functions, bounds, null); | ||||
return makePattern(2, shading, matrix); | return makePattern(2, shading, matrix); | ||||
} | } | ||||
private List<Double> createTransform(MultipleGradientPaint gradient, | |||||
private List<Double> makeTransform(MultipleGradientPaint gradient, | |||||
AffineTransform baseTransform, AffineTransform transform) { | AffineTransform baseTransform, AffineTransform transform) { | ||||
AffineTransform gradientTransform = new AffineTransform(baseTransform); | AffineTransform gradientTransform = new AffineTransform(baseTransform); | ||||
gradientTransform.concatenate(transform); | gradientTransform.concatenate(transform); | ||||
return c.getColorSpace().isCS_sRGB() ? c : ColorUtil.toSRGBColor(c); | return c.getColorSpace().isCS_sRGB() ? c : ColorUtil.toSRGBColor(c); | ||||
} | } | ||||
private List<Double> createBounds(MultipleGradientPaint gradient) { | |||||
private List<Double> makeBounds(MultipleGradientPaint gradient) { | |||||
// TODO is the conversion to double necessary? | // TODO is the conversion to double necessary? | ||||
float[] fractions = gradient.getFractions(); | float[] fractions = gradient.getFractions(); | ||||
List<Double> bounds = new java.util.ArrayList<Double>(fractions.length); | List<Double> bounds = new java.util.ArrayList<Double>(fractions.length); | ||||
return bounds; | return bounds; | ||||
} | } | ||||
private List<Function> createFunctions(MultipleGradientPaint gradient) { | |||||
List<Color> colors = createColors(gradient); | |||||
private List<Function> makeFunctions(MultipleGradientPaint gradient) { | |||||
List<Color> colors = makeColors(gradient); | |||||
List<Function> functions = new ArrayList<Function>(); | List<Function> functions = new ArrayList<Function>(); | ||||
for (int currentPosition = 0, lastPosition = colors.size() - 1; | for (int currentPosition = 0, lastPosition = colors.size() - 1; | ||||
currentPosition < lastPosition; | currentPosition < lastPosition; | ||||
return functions; | return functions; | ||||
} | } | ||||
private List<Color> createColors(MultipleGradientPaint gradient) { | |||||
private List<Color> makeColors(MultipleGradientPaint gradient) { | |||||
Color[] svgColors = gradient.getColors(); | Color[] svgColors = gradient.getColors(); | ||||
List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2); | List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2); | ||||
float[] fractions = gradient.getFractions(); | float[] fractions = gradient.getFractions(); | ||||
List<Double> coords, Function function); | List<Double> coords, Function function); | ||||
protected abstract P makePattern(int patternType, Shading shading, List<Double> matrix); | protected abstract P makePattern(int patternType, Shading shading, List<Double> matrix); | ||||
} | } |
import org.apache.fop.render.pdf.ImageRawCCITTFaxAdapter; | import org.apache.fop.render.pdf.ImageRawCCITTFaxAdapter; | ||||
import org.apache.fop.render.pdf.ImageRawJPEGAdapter; | import org.apache.fop.render.pdf.ImageRawJPEGAdapter; | ||||
import org.apache.fop.render.pdf.ImageRenderedAdapter; | import org.apache.fop.render.pdf.ImageRenderedAdapter; | ||||
import org.apache.fop.render.shading.PDFGradientFactory; | |||||
import org.apache.fop.render.pdf.svg.PDFGradientMaker; | |||||
/** | /** | ||||
* <p>PDF Graphics 2D. | * <p>PDF Graphics 2D. | ||||
gpaint.isCyclic() ? LinearGradientPaint.REPEAT : LinearGradientPaint.NO_CYCLE); | gpaint.isCyclic() ? LinearGradientPaint.REPEAT : LinearGradientPaint.NO_CYCLE); | ||||
} | } | ||||
if (paint instanceof LinearGradientPaint && gradientSupported((LinearGradientPaint) paint)) { | if (paint instanceof LinearGradientPaint && gradientSupported((LinearGradientPaint) paint)) { | ||||
PDFPattern pattern = new PDFGradientFactory(this) | |||||
.createLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
PDFPattern pattern = new PDFGradientMaker(this) | |||||
.makeLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
currentStream.write(pattern.getColorSpaceOut(fill)); | currentStream.write(pattern.getColorSpaceOut(fill)); | ||||
return true; | return true; | ||||
} | } | ||||
if (paint instanceof RadialGradientPaint && gradientSupported((RadialGradientPaint) paint)) { | if (paint instanceof RadialGradientPaint && gradientSupported((RadialGradientPaint) paint)) { | ||||
PDFPattern pattern = new PDFGradientFactory(this) | |||||
.createRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
PDFPattern pattern = new PDFGradientMaker(this) | |||||
.makeRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform()); | |||||
currentStream.write(pattern.getColorSpaceOut(fill)); | currentStream.write(pattern.getColorSpaceOut(fill)); | ||||
return true; | return true; | ||||
} | } |