PiscesRenderingEngine.java revision 999
0N/A/*
0N/A * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage sun.java2d.pisces;
0N/A
0N/Aimport java.awt.Shape;
0N/Aimport java.awt.BasicStroke;
0N/Aimport java.awt.geom.Path2D;
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.PathIterator;
0N/A
0N/Aimport sun.awt.geom.PathConsumer2D;
0N/Aimport sun.java2d.pipe.Region;
0N/Aimport sun.java2d.pipe.RenderingEngine;
0N/Aimport sun.java2d.pipe.AATileGenerator;
0N/A
0N/Apublic class PiscesRenderingEngine extends RenderingEngine {
0N/A public static Transform4 IdentT4 = new Transform4();
0N/A public static double defaultFlat = 0.1;
0N/A
0N/A static int FloatToS15_16(float flt) {
0N/A flt = flt * 65536f + 0.5f;
0N/A if (flt <= -(65536f * 65536f)) {
0N/A return Integer.MIN_VALUE;
0N/A } else if (flt >= (65536f * 65536f)) {
0N/A return Integer.MAX_VALUE;
0N/A } else {
0N/A return (int) Math.floor(flt);
0N/A }
0N/A }
0N/A
0N/A static float S15_16ToFloat(int fix) {
0N/A return (fix / 65536f);
0N/A }
0N/A
0N/A /**
0N/A * Create a widened path as specified by the parameters.
0N/A * <p>
0N/A * The specified {@code src} {@link Shape} is widened according
0N/A * to the specified attribute parameters as per the
0N/A * {@link BasicStroke} specification.
0N/A *
0N/A * @param src the source path to be widened
0N/A * @param width the width of the widened path as per {@code BasicStroke}
0N/A * @param caps the end cap decorations as per {@code BasicStroke}
0N/A * @param join the segment join decorations as per {@code BasicStroke}
0N/A * @param miterlimit the miter limit as per {@code BasicStroke}
0N/A * @param dashes the dash length array as per {@code BasicStroke}
0N/A * @param dashphase the initial dash phase as per {@code BasicStroke}
0N/A * @return the widened path stored in a new {@code Shape} object
0N/A * @since 1.7
0N/A */
0N/A public Shape createStrokedShape(Shape src,
0N/A float width,
0N/A int caps,
0N/A int join,
0N/A float miterlimit,
0N/A float dashes[],
0N/A float dashphase)
0N/A {
0N/A final Path2D p2d = new Path2D.Float();
0N/A
0N/A strokeTo(src,
0N/A null,
0N/A width,
0N/A caps,
0N/A join,
0N/A miterlimit,
0N/A dashes,
0N/A dashphase,
0N/A new LineSink() {
0N/A public void moveTo(int x0, int y0) {
0N/A p2d.moveTo(S15_16ToFloat(x0), S15_16ToFloat(y0));
0N/A }
0N/A public void lineJoin() {}
0N/A public void lineTo(int x1, int y1) {
0N/A p2d.lineTo(S15_16ToFloat(x1), S15_16ToFloat(y1));
0N/A }
0N/A public void close() {
0N/A p2d.closePath();
0N/A }
0N/A public void end() {}
0N/A });
0N/A
0N/A return p2d;
0N/A }
0N/A
0N/A /**
0N/A * Sends the geometry for a widened path as specified by the parameters
0N/A * to the specified consumer.
0N/A * <p>
0N/A * The specified {@code src} {@link Shape} is widened according
0N/A * to the parameters specified by the {@link BasicStroke} object.
0N/A * Adjustments are made to the path as appropriate for the
0N/A * {@link VALUE_STROKE_NORMALIZE} hint if the {@code normalize}
0N/A * boolean parameter is true.
0N/A * Adjustments are made to the path as appropriate for the
0N/A * {@link VALUE_ANTIALIAS_ON} hint if the {@code antialias}
0N/A * boolean parameter is true.
0N/A * <p>
0N/A * The geometry of the widened path is forwarded to the indicated
0N/A * {@link PathConsumer2D} object as it is calculated.
0N/A *
0N/A * @param src the source path to be widened
0N/A * @param bs the {@code BasicSroke} object specifying the
0N/A * decorations to be applied to the widened path
0N/A * @param normalize indicates whether stroke normalization should
0N/A * be applied
0N/A * @param antialias indicates whether or not adjustments appropriate
0N/A * to antialiased rendering should be applied
0N/A * @param consumer the {@code PathConsumer2D} instance to forward
0N/A * the widened geometry to
0N/A * @since 1.7
0N/A */
0N/A public void strokeTo(Shape src,
0N/A AffineTransform at,
0N/A BasicStroke bs,
0N/A boolean thin,
0N/A boolean normalize,
0N/A boolean antialias,
0N/A final PathConsumer2D consumer)
0N/A {
0N/A strokeTo(src, at, bs, thin, normalize, antialias,
0N/A new LineSink() {
0N/A public void moveTo(int x0, int y0) {
0N/A consumer.moveTo(S15_16ToFloat(x0), S15_16ToFloat(y0));
0N/A }
0N/A public void lineJoin() {}
0N/A public void lineTo(int x1, int y1) {
0N/A consumer.lineTo(S15_16ToFloat(x1), S15_16ToFloat(y1));
0N/A }
0N/A public void close() {
0N/A consumer.closePath();
0N/A }
0N/A public void end() {
0N/A consumer.pathDone();
0N/A }
0N/A });
0N/A }
0N/A
0N/A void strokeTo(Shape src,
0N/A AffineTransform at,
0N/A BasicStroke bs,
0N/A boolean thin,
0N/A boolean normalize,
0N/A boolean antialias,
0N/A LineSink lsink)
0N/A {
0N/A float lw;
0N/A if (thin) {
0N/A if (antialias) {
0N/A lw = 0.5f;
0N/A } else {
0N/A lw = 1.0f;
0N/A }
0N/A } else {
0N/A lw = bs.getLineWidth();
0N/A }
0N/A strokeTo(src,
0N/A at,
0N/A lw,
0N/A bs.getEndCap(),
0N/A bs.getLineJoin(),
0N/A bs.getMiterLimit(),
0N/A bs.getDashArray(),
0N/A bs.getDashPhase(),
0N/A lsink);
0N/A }
0N/A
0N/A void strokeTo(Shape src,
0N/A AffineTransform at,
0N/A float width,
0N/A int caps,
0N/A int join,
0N/A float miterlimit,
0N/A float dashes[],
0N/A float dashphase,
0N/A LineSink lsink)
0N/A {
0N/A Transform4 t4;
0N/A
0N/A if (at == null || at.isIdentity()) {
0N/A t4 = IdentT4;
0N/A } else {
0N/A t4 = new Transform4(FloatToS15_16((float) at.getScaleX()),
0N/A FloatToS15_16((float) at.getShearX()),
0N/A FloatToS15_16((float) at.getShearY()),
0N/A FloatToS15_16((float) at.getScaleY()));
0N/A }
0N/A
0N/A lsink = new Stroker(lsink,
0N/A FloatToS15_16(width),
0N/A caps,
0N/A join,
0N/A FloatToS15_16(miterlimit),
0N/A t4);
0N/A if (dashes != null) {
0N/A int fdashes[] = new int[dashes.length];
0N/A for (int i = 0; i < dashes.length; i++) {
0N/A fdashes[i] = FloatToS15_16(dashes[i]);
0N/A }
0N/A lsink = new Dasher(lsink,
0N/A fdashes,
0N/A FloatToS15_16(dashphase),
0N/A t4);
0N/A }
0N/A
0N/A PathIterator pi = src.getPathIterator(at, defaultFlat);
0N/A pathTo(pi, lsink);
0N/A }
0N/A
0N/A void pathTo(PathIterator pi, LineSink lsink) {
0N/A float coords[] = new float[2];
0N/A while (!pi.isDone()) {
0N/A switch (pi.currentSegment(coords)) {
0N/A case PathIterator.SEG_MOVETO:
0N/A lsink.moveTo(FloatToS15_16(coords[0]),
0N/A FloatToS15_16(coords[1]));
0N/A break;
0N/A case PathIterator.SEG_LINETO:
0N/A lsink.lineJoin();
0N/A lsink.lineTo(FloatToS15_16(coords[0]),
0N/A FloatToS15_16(coords[1]));
0N/A break;
0N/A case PathIterator.SEG_CLOSE:
999N/A lsink.lineJoin();
0N/A lsink.close();
0N/A break;
0N/A default:
0N/A throw new InternalError("unknown flattened segment type");
0N/A }
0N/A pi.next();
0N/A }
0N/A lsink.end();
0N/A }
0N/A
0N/A /**
0N/A * Construct an antialiased tile generator for the given shape with
0N/A * the given rendering attributes and store the bounds of the tile
0N/A * iteration in the bbox parameter.
0N/A * The {@code at} parameter specifies a transform that should affect
0N/A * both the shape and the {@code BasicStroke} attributes.
0N/A * The {@code clip} parameter specifies the current clip in effect
0N/A * in device coordinates and can be used to prune the data for the
0N/A * operation, but the renderer is not required to perform any
0N/A * clipping.
0N/A * If the {@code BasicStroke} parameter is null then the shape
0N/A * should be filled as is, otherwise the attributes of the
0N/A * {@code BasicStroke} should be used to specify a draw operation.
0N/A * The {@code thin} parameter indicates whether or not the
0N/A * transformed {@code BasicStroke} represents coordinates smaller
0N/A * than the minimum resolution of the antialiasing rasterizer as
0N/A * specified by the {@code getMinimumAAPenWidth()} method.
0N/A * <p>
0N/A * Upon returning, this method will fill the {@code bbox} parameter
0N/A * with 4 values indicating the bounds of the iteration of the
0N/A * tile generator.
0N/A * The iteration order of the tiles will be as specified by the
0N/A * pseudo-code:
0N/A * <pre>
0N/A * for (y = bbox[1]; y < bbox[3]; y += tileheight) {
0N/A * for (x = bbox[0]; x < bbox[2]; x += tilewidth) {
0N/A * }
0N/A * }
0N/A * </pre>
0N/A * If there is no output to be rendered, this method may return
0N/A * null.
0N/A *
0N/A * @param s the shape to be rendered (fill or draw)
0N/A * @param at the transform to be applied to the shape and the
0N/A * stroke attributes
0N/A * @param clip the current clip in effect in device coordinates
0N/A * @param bs if non-null, a {@code BasicStroke} whose attributes
0N/A * should be applied to this operation
0N/A * @param thin true if the transformed stroke attributes are smaller
0N/A * than the minimum dropout pen width
0N/A * @param normalize true if the {@code VALUE_STROKE_NORMALIZE}
0N/A * {@code RenderingHint} is in effect
0N/A * @param bbox returns the bounds of the iteration
0N/A * @return the {@code AATileGenerator} instance to be consulted
0N/A * for tile coverages, or null if there is no output to render
0N/A * @since 1.7
0N/A */
0N/A public AATileGenerator getAATileGenerator(Shape s,
0N/A AffineTransform at,
0N/A Region clip,
0N/A BasicStroke bs,
0N/A boolean thin,
0N/A boolean normalize,
0N/A int bbox[])
0N/A {
0N/A PiscesCache pc = PiscesCache.createInstance();
0N/A Renderer r = new Renderer();
0N/A r.setCache(pc);
0N/A r.setAntialiasing(3, 3);
0N/A r.beginRendering(clip.getLoX(), clip.getLoY(),
0N/A clip.getWidth(), clip.getHeight());
0N/A if (bs == null) {
0N/A PathIterator pi = s.getPathIterator(at, defaultFlat);
0N/A r.setWindingRule(pi.getWindingRule());
0N/A pathTo(pi, r);
0N/A } else {
0N/A r.setWindingRule(PathIterator.WIND_NON_ZERO);
0N/A strokeTo(s, at, bs, thin, normalize, true, r);
0N/A }
0N/A r.endRendering();
0N/A PiscesTileGenerator ptg = new PiscesTileGenerator(pc, r.MAX_AA_ALPHA);
0N/A ptg.getBbox(bbox);
0N/A return ptg;
0N/A }
0N/A
0N/A /**
0N/A * Returns the minimum pen width that the antialiasing rasterizer
0N/A * can represent without dropouts occuring.
0N/A * @since 1.7
0N/A */
0N/A public float getMinimumAAPenSize() {
0N/A return 0.5f;
0N/A }
0N/A
0N/A static {
0N/A if (PathIterator.WIND_NON_ZERO != Renderer.WIND_NON_ZERO ||
0N/A PathIterator.WIND_EVEN_ODD != Renderer.WIND_EVEN_ODD ||
0N/A BasicStroke.JOIN_MITER != Stroker.JOIN_MITER ||
0N/A BasicStroke.JOIN_ROUND != Stroker.JOIN_ROUND ||
0N/A BasicStroke.JOIN_BEVEL != Stroker.JOIN_BEVEL ||
0N/A BasicStroke.CAP_BUTT != Stroker.CAP_BUTT ||
0N/A BasicStroke.CAP_ROUND != Stroker.CAP_ROUND ||
0N/A BasicStroke.CAP_SQUARE != Stroker.CAP_SQUARE)
0N/A {
0N/A throw new InternalError("mismatched renderer constants");
0N/A }
0N/A }
0N/A}