summaryrefslogtreecommitdiff
path: root/gnu/java/awt
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/java/awt')
-rw-r--r--gnu/java/awt/font/GNUGlyphVector.java2
-rw-r--r--gnu/java/awt/java2d/AbstractGraphics2D.java1002
-rw-r--r--gnu/java/awt/java2d/AlphaCompositeContext.java2
-rw-r--r--gnu/java/awt/java2d/ImagePaint.java192
-rw-r--r--gnu/java/awt/java2d/PolyEdge.java8
-rw-r--r--gnu/java/awt/java2d/RasterGraphics.java103
-rw-r--r--gnu/java/awt/peer/gtk/GdkGraphics.java25
-rw-r--r--gnu/java/awt/peer/swing/SwingComponent.java6
-rw-r--r--gnu/java/awt/peer/swing/SwingComponentPeer.java43
-rw-r--r--gnu/java/awt/peer/swing/SwingContainerPeer.java9
-rw-r--r--gnu/java/awt/peer/swing/SwingFramePeer.java6
-rw-r--r--gnu/java/awt/peer/swing/SwingMenuBarPeer.java2
-rw-r--r--gnu/java/awt/peer/swing/SwingTextFieldPeer.java2
-rw-r--r--gnu/java/awt/peer/swing/SwingWindowPeer.java6
-rw-r--r--gnu/java/awt/print/JavaPrinterGraphics.java518
-rw-r--r--gnu/java/awt/print/JavaPrinterJob.java403
-rw-r--r--gnu/java/awt/print/PostScriptGraphics2D.java1349
-rw-r--r--gnu/java/awt/print/SpooledDocument.java91
18 files changed, 3372 insertions, 397 deletions
diff --git a/gnu/java/awt/font/GNUGlyphVector.java b/gnu/java/awt/font/GNUGlyphVector.java
index 9688698de..f17a45113 100644
--- a/gnu/java/awt/font/GNUGlyphVector.java
+++ b/gnu/java/awt/font/GNUGlyphVector.java
@@ -110,7 +110,7 @@ public class GNUGlyphVector
fontSize = font.getSize2D();
transform = font.getTransform(); // returns a modifiable copy
- transform.concatenate(renderContext.getTransform());
+ //transform.concatenate(renderContext.getTransform());
}
diff --git a/gnu/java/awt/java2d/AbstractGraphics2D.java b/gnu/java/awt/java2d/AbstractGraphics2D.java
index c2a5ac4be..6408a264d 100644
--- a/gnu/java/awt/java2d/AbstractGraphics2D.java
+++ b/gnu/java/awt/java2d/AbstractGraphics2D.java
@@ -84,7 +84,48 @@ import java.util.Iterator;
import java.util.Map;
/**
- * Implements general and shared behaviour for Graphics2D implementation.
+ * This is a 100% Java implementation of the Java2D rendering pipeline. It is
+ * meant as a base class for Graphics2D implementations.
+ *
+ * <h2>Backend interface</h2>
+ * <p>
+ * The backend must at the very least provide a Raster which the the rendering
+ * pipeline can paint into. This must be implemented in
+ * {@link #getDestinationRaster()}. For some backends that might be enough, like
+ * when the target surface can be directly access via the raster (like in
+ * BufferedImages). Other targets need some way to synchronize the raster with
+ * the surface, which can be achieved by implementing the
+ * {@link #updateRaster(Raster, int, int, int, int)} method, which always gets
+ * called after a chunk of data got painted into the raster.
+ * </p>
+ * <p>The backend is free to provide implementations for the various raw*
+ * methods for optimized AWT 1.1 style painting of some primitives. This should
+ * accelerate painting of Swing greatly. When doing so, the backend must also
+ * keep track of the clip and translation, probably by overriding
+ * some clip and translate methods. Don't forget to message super in such a
+ * case.</p>
+ *
+ * <h2>Acceleration options</h2>
+ * <p>
+ * The fact that it is
+ * pure Java makes it a little slow. However, there are several ways of
+ * accelerating the rendering pipeline:
+ * <ol>
+ * <li><em>Optimization hooks for AWT 1.1 - like graphics operations.</em>
+ * The most important methods from the {@link java.awt.Graphics} class
+ * have a corresponding <code>raw*</code> method, which get called when
+ * several optimization conditions are fullfilled. These conditions are
+ * described below. Subclasses can override these methods and delegate
+ * it directly to a native backend.</li>
+ * <li><em>Native PaintContexts and CompositeContext.</em> The implementations
+ * for the 3 PaintContexts and AlphaCompositeContext can be accelerated
+ * using native code. These have proved to two of the most performance
+ * critical points in the rendering pipeline and cannot really be done quickly
+ * in plain Java because they involve lots of shuffling around with large
+ * arrays. In fact, you really would want to let the graphics card to the
+ * work, they are made for this.</li>
+ * </ol>
+ * </p>
*
* @author Roman Kennke (kennke@aicas.com)
*/
@@ -146,11 +187,6 @@ public abstract class AbstractGraphics2D
private Raster paintRaster;
/**
- * A cached pixel array.
- */
- private int[] pixel;
-
- /**
* The raster of the destination surface. This is where the painting is
* performed.
*/
@@ -168,7 +204,7 @@ public abstract class AbstractGraphics2D
private transient ArrayList[] edgeTable;
/**
- * Indicates if cerain graphics primitives can be rendered in an optimized
+ * Indicates if certain graphics primitives can be rendered in an optimized
* fashion. This will be the case if the following conditions are met:
* - The transform may only be a translation, no rotation, shearing or
* scaling.
@@ -198,8 +234,6 @@ public abstract class AbstractGraphics2D
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_DEFAULT);
renderingHints = new RenderingHints(hints);
-
- pixel = new int[4];
}
/**
@@ -212,40 +246,211 @@ public abstract class AbstractGraphics2D
{
// Stroke the shape.
Shape strokedShape = stroke.createStrokedShape(shape);
-
- // Clip the stroked shape.
-// Shape clipped = clipShape(strokedShape);
-// if (clipped != null)
-// {
-// // Fill the shape.
-// fillShape(clipped, false);
-// }
- // FIXME: Clipping doesn't seem to work.
+ // Fill the stroked shape.
fillShape(strokedShape, false);
}
- public boolean drawImage(Image image, AffineTransform xform, ImageObserver obs)
+
+ /**
+ * Draws the specified image and apply the transform for image space ->
+ * user space conversion.
+ *
+ * This method is implemented to special case RenderableImages and
+ * RenderedImages and delegate to
+ * {@link #drawRenderableImage(RenderableImage, AffineTransform)} and
+ * {@link #drawRenderedImage(RenderedImage, AffineTransform)} accordingly.
+ * Other image types are not yet handled.
+ *
+ * @param image the image to be rendered
+ * @param xform the transform from image space to user space
+ * @param obs the image observer to be notified
+ */
+ public boolean drawImage(Image image, AffineTransform xform,
+ ImageObserver obs)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ boolean ret = false;
+ Rectangle areaOfInterest = new Rectangle(0, 0, image.getWidth(obs),
+ image.getHeight(obs));
+ return drawImageImpl(image, xform, obs, areaOfInterest);
+ }
+
+ /**
+ * Draws the specified image and apply the transform for image space ->
+ * user space conversion. This method only draw the part of the image
+ * specified by <code>areaOfInterest</code>.
+ *
+ * This method is implemented to special case RenderableImages and
+ * RenderedImages and delegate to
+ * {@link #drawRenderableImage(RenderableImage, AffineTransform)} and
+ * {@link #drawRenderedImage(RenderedImage, AffineTransform)} accordingly.
+ * Other image types are not yet handled.
+ *
+ * @param image the image to be rendered
+ * @param xform the transform from image space to user space
+ * @param obs the image observer to be notified
+ * @param areaOfInterest the area in image space that is rendered
+ */
+ private boolean drawImageImpl(Image image, AffineTransform xform,
+ ImageObserver obs, Rectangle areaOfInterest)
+ {
+ boolean ret;
+ if (image == null)
+ {
+ ret = true;
+ }
+ else if (image instanceof RenderedImage)
+ {
+ // FIXME: Handle the ImageObserver.
+ drawRenderedImageImpl((RenderedImage) image, xform, areaOfInterest);
+ ret = true;
+ }
+ else if (image instanceof RenderableImage)
+ {
+ // FIXME: Handle the ImageObserver.
+ drawRenderableImageImpl((RenderableImage) image, xform, areaOfInterest);
+ ret = true;
+ }
+ else
+ {
+ // FIXME: Implement rendering of other Image types.
+ ret = false;
+ }
+ return ret;
}
+ /**
+ * Renders a BufferedImage and applies the specified BufferedImageOp before
+ * to filter the BufferedImage somehow. The resulting BufferedImage is then
+ * passed on to {@link #drawRenderedImage(RenderedImage, AffineTransform)}
+ * to perform the final rendering.
+ *
+ * @param image the source buffered image
+ * @param op the filter to apply to the buffered image before rendering
+ * @param x the x coordinate to render the image to
+ * @param y the y coordinate to render the image to
+ */
public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ BufferedImage filtered =
+ op.createCompatibleDestImage(image, image.getColorModel());
+ AffineTransform t = new AffineTransform();
+ t.translate(x, y);
+ drawRenderedImage(filtered, t);
}
+ /**
+ * Renders the specified image to the destination raster. The specified
+ * transform is used to convert the image into user space. The transform
+ * of this AbstractGraphics2D object is used to transform from user space
+ * to device space.
+ *
+ * The rendering is performed using the scanline algorithm that performs the
+ * rendering of other shapes and a custom Paint implementation, that supplies
+ * the pixel values of the rendered image.
+ *
+ * @param image the image to render to the destination raster
+ * @param xform the transform from image space to user space
+ */
public void drawRenderedImage(RenderedImage image, AffineTransform xform)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ Rectangle areaOfInterest = new Rectangle(image.getMinX(),
+ image.getHeight(),
+ image.getWidth(),
+ image.getHeight());
+ drawRenderedImageImpl(image, xform, areaOfInterest);
+ }
+
+ /**
+ * Renders the specified image to the destination raster. The specified
+ * transform is used to convert the image into user space. The transform
+ * of this AbstractGraphics2D object is used to transform from user space
+ * to device space. Only the area specified by <code>areaOfInterest</code>
+ * is finally rendered to the target.
+ *
+ * The rendering is performed using the scanline algorithm that performs the
+ * rendering of other shapes and a custom Paint implementation, that supplies
+ * the pixel values of the rendered image.
+ *
+ * @param image the image to render to the destination raster
+ * @param xform the transform from image space to user space
+ */
+ private void drawRenderedImageImpl(RenderedImage image,
+ AffineTransform xform,
+ Rectangle areaOfInterest)
+ {
+ // First we compute the transformation. This is made up of 3 parts:
+ // 1. The areaOfInterest -> image space transform.
+ // 2. The image space -> user space transform.
+ // 3. The user space -> device space transform.
+ AffineTransform t = new AffineTransform();
+ t.translate(- areaOfInterest.x - image.getMinX(),
+ - areaOfInterest.y - image.getMinY());
+ t.concatenate(xform);
+ t.concatenate(transform);
+ AffineTransform it = null;
+ try
+ {
+ it = t.createInverse();
+ }
+ catch (NoninvertibleTransformException ex)
+ {
+ // Ignore -- we return if the transform is not invertible.
+ }
+ if (it != null)
+ {
+ // Transform the area of interest into user space.
+ GeneralPath aoi = new GeneralPath(areaOfInterest);
+ aoi.transform(xform);
+ // Render the shape using the standard renderer, but with a temporary
+ // ImagePaint.
+ ImagePaint p = new ImagePaint(image, it);
+ Paint savedPaint = paint;
+ try
+ {
+ paint = p;
+ fillShape(aoi, false);
+ }
+ finally
+ {
+ paint = savedPaint;
+ }
+ }
}
+ /**
+ * Renders a renderable image. This produces a RenderedImage, which is
+ * then passed to {@link #drawRenderedImage(RenderedImage, AffineTransform)}
+ * to perform the final rendering.
+ *
+ * @param image the renderable image to be rendered
+ * @param xform the transform from image space to user space
+ */
public void drawRenderableImage(RenderableImage image, AffineTransform xform)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ Rectangle areaOfInterest = new Rectangle((int) image.getMinX(),
+ (int) image.getHeight(),
+ (int) image.getWidth(),
+ (int) image.getHeight());
+ drawRenderableImageImpl(image, xform, areaOfInterest);
+
+ }
+
+ /**
+ * Renders a renderable image. This produces a RenderedImage, which is
+ * then passed to {@link #drawRenderedImage(RenderedImage, AffineTransform)}
+ * to perform the final rendering. Only the area of the image specified
+ * by <code>areaOfInterest</code> is rendered.
+ *
+ * @param image the renderable image to be rendered
+ * @param xform the transform from image space to user space
+ */
+ private void drawRenderableImageImpl(RenderableImage image,
+ AffineTransform xform,
+ Rectangle areaOfInterest)
+ {
+ // TODO: Maybe make more clever usage of a RenderContext here.
+ RenderedImage rendered = image.createDefaultRendering();
+ drawRenderedImageImpl(rendered, xform, areaOfInterest);
}
/**
@@ -257,9 +462,14 @@ public abstract class AbstractGraphics2D
*/
public void drawString(String text, int x, int y)
{
- FontRenderContext ctx = getFontRenderContext();
- GlyphVector gv = font.createGlyphVector(ctx, text.toCharArray());
- drawGlyphVector(gv, x, y);
+ if (isOptimized)
+ rawDrawString(text, x, y);
+ else
+ {
+ FontRenderContext ctx = getFontRenderContext();
+ GlyphVector gv = font.createGlyphVector(ctx, text.toCharArray());
+ drawGlyphVector(gv, x, y);
+ }
}
/**
@@ -313,9 +523,6 @@ public abstract class AbstractGraphics2D
*/
public void fill(Shape shape)
{
-// Shape clipped = clipShape(shape);
-// if (clipped != null)
-// fillShape(clipped, false);
fillShape(shape, false);
}
@@ -355,7 +562,6 @@ public abstract class AbstractGraphics2D
else
{
updateOptimization();
- rawSetForeground((Color) paint);
}
}
}
@@ -537,7 +743,7 @@ public abstract class AbstractGraphics2D
if (clip != null)
{
AffineTransform clipTransform = new AffineTransform();
- clipTransform.scale(-scaleX, -scaleY);
+ clipTransform.scale(1 / scaleX, 1 / scaleY);
updateClip(clipTransform);
}
updateOptimization();
@@ -712,8 +918,7 @@ public abstract class AbstractGraphics2D
public FontRenderContext getFontRenderContext()
{
- //return new FontRenderContext(transform, false, false);
- return new FontRenderContext(new AffineTransform(), false, false);
+ return new FontRenderContext(transform, false, true);
}
/**
@@ -726,22 +931,15 @@ public abstract class AbstractGraphics2D
public void drawGlyphVector(GlyphVector gv, float x, float y)
{
int numGlyphs = gv.getNumGlyphs();
- AffineTransform t = new AffineTransform();
- t.translate(x, y);
-
-// // TODO: We could use fill(gv.getOutline()), but that seems to be
- // slightly more inefficient.
+ translate(x, y);
+ // TODO: We could use fill(gv.getOutline()), but that seems to be
+ // slightly more inefficient.
for (int i = 0; i < numGlyphs; i++)
{
- //fill(gv.getGlyphVisualBounds(i));
- GeneralPath p = new GeneralPath(gv.getGlyphOutline(i));
- p.transform(t);
- //Shape clipped = clipShape(p);
- //if (clipped != null)
- // fillShape(clipped, true);
- // FIXME: Clipping doesn't seem to work correctly.
- fillShape(p, true);
+ Shape o = gv.getGlyphOutline(i);
+ fillShape(o, true);
}
+ translate(-x, -y);
}
/**
@@ -919,8 +1117,10 @@ public abstract class AbstractGraphics2D
public void copyArea(int x, int y, int width, int height, int dx, int dy)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ if (isOptimized)
+ rawCopyArea(x, y, width, height, dx, dy);
+ else
+ copyAreaImpl(x, y, width, height, dx, dy);
}
/**
@@ -979,11 +1179,15 @@ public abstract class AbstractGraphics2D
*/
public void clearRect(int x, int y, int width, int height)
{
- Paint savedForeground = getPaint();
- setPaint(getBackground());
- //System.err.println("clearRect transform type: " + transform.getType());
- fillRect(x, y, width, height);
- setPaint(savedForeground);
+ if (isOptimized)
+ rawClearRect(x, y, width, height);
+ else
+ {
+ Paint savedForeground = getPaint();
+ setPaint(getBackground());
+ fillRect(x, y, width, height);
+ setPaint(savedForeground);
+ }
}
/**
@@ -1088,47 +1292,153 @@ public abstract class AbstractGraphics2D
fill(new Polygon(xPoints, yPoints, npoints));
}
+ /**
+ * Draws the specified image at the specified location. This forwards
+ * to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
+ *
+ * @param image the image to render
+ * @param x the x location to render to
+ * @param y the y location to render to
+ * @param observer the image observer to receive notification
+ */
public boolean drawImage(Image image, int x, int y, ImageObserver observer)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ boolean ret;
+ if (isOptimized)
+ ret = rawDrawImage(image, x, y, observer);
+ else
+ {
+ AffineTransform t = new AffineTransform();
+ t.translate(x, y);
+ ret = drawImage(image, t, observer);
+ }
+ return ret;
}
+ /**
+ * Draws the specified image at the specified location. The image
+ * is scaled to the specified width and height. This forwards
+ * to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
+ *
+ * @param image the image to render
+ * @param x the x location to render to
+ * @param y the y location to render to
+ * @param width the target width of the image
+ * @param height the target height of the image
+ * @param observer the image observer to receive notification
+ */
public boolean drawImage(Image image, int x, int y, int width, int height,
ImageObserver observer)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ AffineTransform t = new AffineTransform();
+ t.translate(x, y);
+ double scaleX = (double) image.getWidth(observer) / (double) width;
+ double scaleY = (double) image.getHeight(observer) / (double) height;
+ t.scale(scaleX, scaleY);
+ return drawImage(image, t, observer);
}
+ /**
+ * Draws the specified image at the specified location. This forwards
+ * to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
+ *
+ * @param image the image to render
+ * @param x the x location to render to
+ * @param y the y location to render to
+ * @param bgcolor the background color to use for transparent pixels
+ * @param observer the image observer to receive notification
+ */
public boolean drawImage(Image image, int x, int y, Color bgcolor,
ImageObserver observer)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ AffineTransform t = new AffineTransform();
+ t.translate(x, y);
+ // TODO: Somehow implement the background option.
+ return drawImage(image, t, observer);
}
+ /**
+ * Draws the specified image at the specified location. The image
+ * is scaled to the specified width and height. This forwards
+ * to {@link #drawImage(Image, AffineTransform, ImageObserver)}.
+ *
+ * @param image the image to render
+ * @param x the x location to render to
+ * @param y the y location to render to
+ * @param width the target width of the image
+ * @param height the target height of the image
+ * @param bgcolor the background color to use for transparent pixels
+ * @param observer the image observer to receive notification
+ */
public boolean drawImage(Image image, int x, int y, int width, int height,
Color bgcolor, ImageObserver observer)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ AffineTransform t = new AffineTransform();
+ t.translate(x, y);
+ double scaleX = (double) image.getWidth(observer) / (double) width;
+ double scaleY = (double) image.getHeight(observer) / (double) height;
+ t.scale(scaleX, scaleY);
+ // TODO: Somehow implement the background option.
+ return drawImage(image, t, observer);
}
+ /**
+ * Draws an image fragment to a rectangular area of the target.
+ *
+ * @param image the image to render
+ * @param dx1 the first corner of the destination rectangle
+ * @param dy1 the first corner of the destination rectangle
+ * @param dx2 the second corner of the destination rectangle
+ * @param dy2 the second corner of the destination rectangle
+ * @param sx1 the first corner of the source rectangle
+ * @param sy1 the first corner of the source rectangle
+ * @param sx2 the second corner of the source rectangle
+ * @param sy2 the second corner of the source rectangle
+ * @param observer the image observer to be notified
+ */
public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2,
ImageObserver observer)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ int sx = Math.min(sx1, sx1);
+ int sy = Math.min(sy1, sy2);
+ int sw = Math.abs(sx1 - sx2);
+ int sh = Math.abs(sy1 - sy2);
+ int dx = Math.min(dx1, dx1);
+ int dy = Math.min(dy1, dy2);
+ int dw = Math.abs(dx1 - dx2);
+ int dh = Math.abs(dy1 - dy2);
+
+ AffineTransform t = new AffineTransform();
+ t.translate(sx - dx, sy - dy);
+ double scaleX = (double) sw / (double) dw;
+ double scaleY = (double) sh / (double) dh;
+ t.scale(scaleX, scaleY);
+ Rectangle areaOfInterest = new Rectangle(sx, sy, sw, sh);
+ return drawImageImpl(image, t, observer, areaOfInterest);
}
+ /**
+ * Draws an image fragment to a rectangular area of the target.
+ *
+ * @param image the image to render
+ * @param dx1 the first corner of the destination rectangle
+ * @param dy1 the first corner of the destination rectangle
+ * @param dx2 the second corner of the destination rectangle
+ * @param dy2 the second corner of the destination rectangle
+ * @param sx1 the first corner of the source rectangle
+ * @param sy1 the first corner of the source rectangle
+ * @param sx2 the second corner of the source rectangle
+ * @param sy2 the second corner of the source rectangle
+ * @param bgcolor the background color to use for transparent pixels
+ * @param observer the image observer to be notified
+ */
public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2, Color bgcolor,
ImageObserver observer)
{
- // FIXME: Implement this.
- throw new UnsupportedOperationException("Not yet implemented");
+ // FIXME: Do something with bgcolor.
+ return drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
}
/**
@@ -1155,8 +1465,8 @@ public abstract class AbstractGraphics2D
Object v = renderingHints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
// We default to antialiasing on for text as long as we have no
// good hinting implemented.
- antialias = (v == RenderingHints.VALUE_TEXT_ANTIALIAS_ON
- || v == RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
+ antialias = (v == RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+ //|| v == RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
}
else
{
@@ -1164,237 +1474,150 @@ public abstract class AbstractGraphics2D
antialias = (v == RenderingHints.VALUE_ANTIALIAS_ON);
}
- Rectangle2D userBounds = s.getBounds2D();
-
- // Flatten the path. TODO: Determine the best flattening factor
- // wrt to speed and quality.
- PathIterator path = s.getPathIterator(getTransform(), 1.0);
-
- // Build up polygons and let the native backend render this using
- // rawFillShape() which would provide a default implementation for
- // drawPixel using a PolyScan algorithm.
- double[] seg = new double[6];
-
- // TODO: Use ArrayList<PolyEdge> here when availble.
- ArrayList segs = new ArrayList();
- double segX = 0.; // The start point of the current edge.
- double segY = 0.;
- double polyX = 0.; // The start point of the current polygon.
- double polyY = 0.;
-
- double minX = Integer.MAX_VALUE;
- double maxX = Integer.MIN_VALUE;
- double minY = Integer.MAX_VALUE;
- double maxY = Integer.MIN_VALUE;
+ double offs = 0.5;
+ if (antialias)
+ offs = offs / AA_SAMPLING;
- //System.err.println("fill polygon");
- while (! path.isDone())
- {
- int segType = path.currentSegment(seg);
- minX = Math.min(minX, seg[0]);
- maxX = Math.max(maxX, seg[0]);
- minY = Math.min(minY, seg[1]);
- maxY = Math.max(maxY, seg[1]);
-
- //System.err.println("segment: " + segType + ", " + seg[0] + ", " + seg[1]);
- if (segType == PathIterator.SEG_MOVETO)
- {
- segX = seg[0];
- segY = seg[1];
- polyX = seg[0];
- polyY = seg[1];
- }
- else if (segType == PathIterator.SEG_CLOSE)
- {
- // Close the polyline.
- PolyEdge edge = new PolyEdge(segX, segY, polyX, polyY);
- segs.add(edge);
- }
- else if (segType == PathIterator.SEG_LINETO)
- {
- PolyEdge edge = new PolyEdge(segX, segY, seg[0], seg[1]);
- segs.add(edge);
- segX = seg[0];
- segY = seg[1];
- }
- path.next();
- }
+ Rectangle2D userBounds = s.getBounds2D();
+ Rectangle2D deviceBounds = new Rectangle2D.Double();
+ ArrayList segs = getSegments(s, transform, deviceBounds, false, offs);
+ Rectangle2D clipBounds = new Rectangle2D.Double();
+ ArrayList clipSegs = getSegments(clip, transform, clipBounds, true, offs);
+ segs.addAll(clipSegs);
+ Rectangle2D inclClipBounds = new Rectangle2D.Double();
+ Rectangle2D.union(clipBounds, deviceBounds, inclClipBounds);
if (segs.size() > 0)
{
if (antialias)
- fillShapeAntialias(segs, minX, minY, maxX, maxY, userBounds);
+ fillShapeAntialias(segs, deviceBounds, userBounds, inclClipBounds);
else
- rawFillShape(segs, minX, minY, maxX, maxY, userBounds);
+ fillShapeImpl(segs, deviceBounds, userBounds, inclClipBounds);
}
}
/**
- * Draws one pixel in the target coordinate space. This method draws the
- * specified pixel by getting the painting pixel for that coordinate
- * from the paintContext and compositing the pixel with the compositeContext.
- * The resulting pixel is then set by calling {@link #rawSetPixel}.
+ * Returns the color model of this Graphics object.
*
- * @param x the x coordinate
- * @param y the y coordinate
+ * @return the color model of this Graphics object
*/
- protected void drawPixel(int x, int y)
- {
- // FIXME: Implement efficient compositing.
- if (! (paint instanceof Color))
- {
- int[] paintPixel = paintRaster.getPixel(x, y, pixel);
- Color c = new Color(paintPixel[0], paintPixel[1], paintPixel[2]);
- rawSetForeground(c);
- }
- rawSetPixel(x, y);
- }
+ protected abstract ColorModel getColorModel();
/**
- * Draws a pixel in the target coordinate space using the specified color.
- *
- * @param x the x coordinate
- * @param y the y coordinate
+ * Returns the bounds of the target.
+ *
+ * @return the bounds of the target
*/
- protected void rawSetPixel(int x, int y)
+ protected Rectangle getDeviceBounds()
{
- // FIXME: Provide default implementation or remove method.
+ return destinationRaster.getBounds();
}
/**
- * Sets the foreground color for drawing.
+ * Draws a line in optimization mode. The implementation should respect the
+ * clip and translation. It can assume that the clip is a rectangle and that
+ * the transform is only a translating transform.
*
- * @param c the color to set
+ * @param x0 the starting point, X coordinate
+ * @param y0 the starting point, Y coordinate
+ * @param x1 the end point, X coordinate
+ * @param y1 the end point, Y coordinate
*/
- protected void rawSetForeground(Color c)
- {
- // Probably remove method.
- }
-
- protected void rawSetForeground(int r, int g, int b)
+ protected void rawDrawLine(int x0, int y0, int x1, int y1)
{
- rawSetForeground(new Color(r, g, b));
+ draw(new Line2D.Float(x0, y0, x1, y1));
}
/**
- * Returns the color model of this Graphics object.
+ * Draws a string in optimization mode. The implementation should respect the
+ * clip and translation. It can assume that the clip is a rectangle and that
+ * the transform is only a translating transform.
*
- * @return the color model of this Graphics object
+ * @param text the string to be drawn
+ * @param x the start of the baseline, X coordinate
+ * @param y the start of the baseline, Y coordinate
*/
- protected abstract ColorModel getColorModel();
+ protected void rawDrawString(String text, int x, int y)
+ {
+ FontRenderContext ctx = getFontRenderContext();
+ GlyphVector gv = font.createGlyphVector(ctx, text.toCharArray());
+ drawGlyphVector(gv, x, y);
+ }
/**
- * Returns the bounds of the target.
+ * Clears a rectangle in optimization mode. The implementation should respect the
+ * clip and translation. It can assume that the clip is a rectangle and that
+ * the transform is only a translating transform.
*
- * @return the bounds of the target
+ * @param x the upper left corner, X coordinate
+ * @param y the upper left corner, Y coordinate
+ * @param w the width
+ * @param h the height
*/
- protected Rectangle getDeviceBounds()
+ protected void rawClearRect(int x, int y, int w, int h)
{
- return destinationRaster.getBounds();
+ Paint savedForeground = getPaint();
+ setPaint(getBackground());
+ rawFillRect(x, y, w, h);
+ setPaint(savedForeground);
}
/**
- * Returns the bounds of the drawing area in user space.
+ * Fills a rectangle in optimization mode. The implementation should respect
+ * the clip but can assume that it is a rectangle.
*
- * @return the bounds of the drawing area in user space
+ * @param x the upper left corner, X coordinate
+ * @param y the upper left corner, Y coordinate
+ * @param w the width
+ * @param h the height
*/
- protected Rectangle2D getUserBounds()
+ protected void rawFillRect(int x, int y, int w, int h)
{
- PathIterator pathIter = getDeviceBounds().getPathIterator(getTransform());
- GeneralPath path = new GeneralPath();
- path.append(pathIter, true);
- return path.getBounds();
-
+ fill(new Rectangle(x, y, w, h));
}
+
/**
- * Draws a line in optimization mode. The implementation should respect the
- * clip but can assume that it is a rectangle.
+ * Draws an image in optimization mode. The implementation should respect
+ * the clip but can assume that it is a rectangle.
*
- * @param x0 the starting point, X coordinate
- * @param y0 the starting point, Y coordinate
- * @param x1 the end point, X coordinate
- * @param y1 the end point, Y coordinate
+ * @param image the image to be painted
+ * @param x the location, X coordinate
+ * @param y the location, Y coordinate
+ * @param obs the image observer to be notified
+ *
+ * @return <code>true</code> when the image is painted completely,
+ * <code>false</code> if it is still rendered
*/
- protected void rawDrawLine(int x0, int y0, int x1, int y1)
+ protected boolean rawDrawImage(Image image, int x, int y, ImageObserver obs)
{
- // This is an implementation of Bresenham's line drawing algorithm.
- int dy = y1 - y0;
- int dx = x1 - x0;
- int stepx, stepy;
-
- if (dy < 0)
- {
- dy = -dy;
- stepy = -1;
- }
- else
- {
- stepy = 1;
- }
- if (dx < 0)
- {
- dx = -dx;
- stepx = -1;
- }
- else
- {
- stepx = 1;
- }
- dy <<= 1;
- dx <<= 1;
-
- drawPixel(x0, y0);
- if (dx > dy)
- {
- int fraction = dy - (dx >> 1); // same as 2*dy - dx
- while (x0 != x1)
- {
- if (fraction >= 0)
- {
- y0 += stepy;
- fraction -= dx;
- }
- x0 += stepx;
- fraction += dy;
- drawPixel(x0, y0);
- }
- }
- else
- {
- int fraction = dx - (dy >> 1);
- while (y0 != y1)
- {
- if (fraction >= 0)
- {
- x0 += stepx;
- fraction -= dy;
- }
- y0 += stepy;
- fraction += dx;
- drawPixel(x0, y0);
- }
- }
+ AffineTransform t = new AffineTransform();
+ t.translate(x, y);
+ return drawImage(image, t, obs);
}
/**
- * Fills a rectangle in optimization mode. The implementation should respect
- * the clip but can assume that it is a rectangle.
+ * Copies a rectangular region to another location.
*
* @param x the upper left corner, X coordinate
* @param y the upper left corner, Y coordinate
* @param w the width
* @param h the height
+ * @param dx
+ * @param dy
*/
- protected void rawFillRect(int x, int y, int w, int h)
+ protected void rawCopyArea(int x, int y, int w, int h, int dx, int dy)
{
- int x2 = x + w;
- int y2 = y + h;
- for (int xc = x; xc < x2; xc++)
- {
- for (int yc = y; yc < y2; yc++)
- {
- drawPixel(xc, yc);
- }
- }
+ copyAreaImpl(x, y, w, h, dx, dy);
+ }
+
+ // Private implementation methods.
+
+ /**
+ * Copies a rectangular area of the target raster to a different location.
+ */
+ private void copyAreaImpl(int x, int y, int w, int h, int dx, int dy)
+ {
+ // FIXME: Implement this properly.
+ throw new UnsupportedOperationException("Not implemented yet.");
}
/**
@@ -1404,8 +1627,9 @@ public abstract class AbstractGraphics2D
*
* The polygon is already clipped when this method is called.
*/
- protected void rawFillShape(ArrayList segs, double minX, double minY,
- double maxX, double maxY, Rectangle2D userBounds)
+ private void fillShapeImpl(ArrayList segs, Rectangle2D deviceBounds2D,
+ Rectangle2D userBounds,
+ Rectangle2D inclClipBounds)
{
// This is an implementation of a polygon scanline conversion algorithm
// described here:
@@ -1414,19 +1638,25 @@ public abstract class AbstractGraphics2D
// Create table of all edges.
// The edge buckets, sorted and indexed by their Y values.
+ double minX = deviceBounds2D.getMinX();
+ double minY = deviceBounds2D.getMinY();
+ double maxX = deviceBounds2D.getMaxX();
+ double maxY = deviceBounds2D.getMaxY();
+ double icMinY = inclClipBounds.getMinY();
+ double icMaxY = inclClipBounds.getMaxY();
Rectangle deviceBounds = new Rectangle((int) minX, (int) minY,
(int) Math.ceil(maxX) - (int) minX,
(int) Math.ceil(maxY) - (int) minY);
PaintContext pCtx = paint.createContext(getColorModel(), deviceBounds,
userBounds, transform, renderingHints);
- ArrayList[] edgeTable = new ArrayList[(int) Math.ceil(maxY)
- - (int) Math.ceil(minY) + 1];
+ ArrayList[] edgeTable = new ArrayList[(int) Math.ceil(icMaxY)
+ - (int) Math.ceil(icMinY) + 1];
for (Iterator i = segs.iterator(); i.hasNext();)
{
PolyEdge edge = (PolyEdge) i.next();
- int yindex = (int) ((int) Math.ceil(edge.y0) - (int) Math.ceil(minY));
+ int yindex = (int) ((int) Math.ceil(edge.y0) - (int) Math.ceil(icMinY));
if (edgeTable[yindex] == null) // Create bucket when needed.
edgeTable[yindex] = new ArrayList();
edgeTable[yindex].add(edge); // Add edge to the bucket of its line.
@@ -1446,7 +1676,7 @@ public abstract class AbstractGraphics2D
PolyEdgeComparator comparator = new PolyEdgeComparator();
// Scan all relevant lines.
- int minYInt = (int) Math.ceil(minY);
+ int minYInt = (int) Math.ceil(icMinY);
for (int y = minYInt; y <= maxY; y++)
{
ArrayList bucket = edgeTable[y - minYInt];
@@ -1497,35 +1727,30 @@ public abstract class AbstractGraphics2D
// Now draw all pixels inside the polygon.
// This is the last edge that intersected the scanline.
PolyEdge previous = null; // Gets initialized below.
- boolean active = false;
+ boolean insideShape = false;
+ boolean insideClip = false;
//System.err.println("scanline: " + y);
for (Iterator i = activeEdges.iterator(); i.hasNext();)
{
PolyEdge edge = (PolyEdge) i.next();
- // Only fill scanline, if the current edge actually intersects
- // the scanline. There may be edges that lie completely
- // within the current scanline.
- //System.err.println("previous: " + previous);
- //System.err.println("edge: " + edge);
- if (active)
+ if (edge.y1 <= y)
+ continue;
+
+ // Draw scanline when we are inside the shape AND inside the
+ // clip.
+ if (insideClip && insideShape)
{
- if (edge.y1 > y)
- {
- int x0 = (int) previous.xIntersection;
- int x1 = (int) edge.xIntersection;
- fillScanline(pCtx, x0, x1, y);
- previous = edge;
- active = false;
- }
+ int x0 = (int) previous.xIntersection;
+ int x1 = (int) edge.xIntersection;
+ if (x0 < x1)
+ fillScanline(pCtx, x0, x1, y);
}
+ // Update state.
+ previous = edge;
+ if (edge.isClip)
+ insideClip = ! insideClip;
else
- {
- if (edge.y1 > y)
- {
- previous = edge;
- active = true;
- }
- }
+ insideShape = ! insideShape;
}
}
pCtx.dispose();
@@ -1545,7 +1770,8 @@ public abstract class AbstractGraphics2D
CompositeContext cCtx = composite.createContext(paintColorModel,
getColorModel(),
renderingHints);
- cCtx.compose(paintRaster, destinationRaster, destinationRaster);
+ WritableRaster targetChild = destinationRaster.createWritableTranslatedChild(-x0,- y);
+ cCtx.compose(paintRaster, targetChild, targetChild);
updateRaster(destinationRaster, x0, y, x1 - x0, 1);
cCtx.dispose();
}
@@ -1554,14 +1780,10 @@ public abstract class AbstractGraphics2D
* Fills arbitrary shapes in an anti-aliased fashion.
*
* @param segs the line segments which define the shape which is to be filled
- * @param minX the bounding box, left X
- * @param minY the bounding box, upper Y
- * @param maxX the bounding box, right X
- * @param maxY the bounding box, lower Y
*/
- private void fillShapeAntialias(ArrayList segs, double minX, double minY,
- double maxX, double maxY,
- Rectangle2D userBounds)
+ private void fillShapeAntialias(ArrayList segs, Rectangle2D deviceBounds2D,
+ Rectangle2D userBounds,
+ Rectangle2D inclClipBounds)
{
// This is an implementation of a polygon scanline conversion algorithm
// described here:
@@ -1569,23 +1791,32 @@ public abstract class AbstractGraphics2D
// The antialiasing is implemented using a sampling technique, we do
// not scan whole lines but fractions of the line.
+ double minX = deviceBounds2D.getMinX();
+ double minY = deviceBounds2D.getMinY();
+ double maxX = deviceBounds2D.getMaxX();
+ double maxY = deviceBounds2D.getMaxY();
+ double icMinY = inclClipBounds.getMinY();
+ double icMaxY = inclClipBounds.getMaxY();
+ double icMinX = inclClipBounds.getMinX();
+ double icMaxX = inclClipBounds.getMaxX();
Rectangle deviceBounds = new Rectangle((int) minX, (int) minY,
(int) Math.ceil(maxX) - (int) minX,
(int) Math.ceil(maxY) - (int) minY);
- PaintContext pCtx = paint.createContext(getColorModel(), deviceBounds,
+ PaintContext pCtx = paint.createContext(ColorModel.getRGBdefault(),
+ deviceBounds,
userBounds, transform,
renderingHints);
// This array will contain the oversampled transparency values for
// each pixel in the scanline.
- int numScanlines = (int) Math.ceil(maxY) - (int) minY;
- int numScanlinePixels = (int) Math.ceil(maxX) - (int) minX + 1;
+ int numScanlines = (int) Math.ceil(icMaxY) - (int) icMinY;
+ int numScanlinePixels = (int) Math.ceil(icMaxX) - (int) icMinX + 1;
if (alpha == null || alpha.length < (numScanlinePixels + 1))
alpha = new int[numScanlinePixels + 1];
- int firstLine = (int) minY;
+ int firstLine = (int) icMinY;
//System.err.println("minY: " + minY);
- int firstSubline = (int) (Math.ceil((minY - Math.floor(minY)) * AA_SAMPLING));
+ int firstSubline = (int) (Math.ceil((icMinY - Math.floor(icMinY)) * AA_SAMPLING));
double firstLineDouble = firstLine + firstSubline / (double) AA_SAMPLING;
//System.err.println("firstSubline: " + firstSubline);
@@ -1630,8 +1861,11 @@ public abstract class AbstractGraphics2D
// Scan all lines.
int yindex = 0;
//System.err.println("firstLine: " + firstLine + ", maxY: " + maxY + ", firstSubline: " + firstSubline);
- for (int y = firstLine; y <= maxY; y++)
+ for (int y = firstLine; y <= icMaxY; y++)
{
+ int leftX = (int) icMaxX;
+ int rightX = (int) icMinX;
+ boolean emptyScanline = true;
for (int subY = firstSubline; subY < AA_SAMPLING; subY++)
{
//System.err.println("scanline: " + y + ", subScanline: " + subY);
@@ -1688,17 +1922,16 @@ public abstract class AbstractGraphics2D
// Now draw all pixels inside the polygon.
// This is the last edge that intersected the scanline.
PolyEdge previous = null; // Gets initialized below.
- boolean active = false;
+ boolean insideClip = false;
+ boolean insideShape = false;
//System.err.println("scanline: " + y + ", subscanline: " + subY);
for (Iterator i = activeEdges.iterator(); i.hasNext();)
{
PolyEdge edge = (PolyEdge) i.next();
- // Only fill scanline, if the current edge actually intersects
- // the scanline. There may be edges that lie completely
- // within the current scanline.
- //System.err.println("previous: " + previous);
- //System.err.println("edge: " + edge);
- if (active)
+ if (edge.y1 <= (y + (subY / (double) AA_SAMPLING)))
+ continue;
+
+ if (insideClip && insideShape)
{
// TODO: Use integer arithmetics here.
if (edge.y1 > (y + (subY / (double) AA_SAMPLING)))
@@ -1709,32 +1942,30 @@ public abstract class AbstractGraphics2D
int x1 = (int) Math.min(Math.max(edge.xIntersection, minX), maxX);
//System.err.println("minX: " + minX + ", x0: " + x0 + ", x1: " + x1 + ", maxX: " + maxX);
// TODO: Pull out cast.
- alpha[x0 - (int) minX]++;
- alpha[x1 - (int) minX + 1]--;
- previous = edge;
- active = false;
+ int left = x0 - (int) minX;
+ int right = x1 - (int) minX + 1;
+ alpha[left]++;
+ alpha[right]--;
+ leftX = Math.min(x0, leftX);
+ rightX = Math.max(x1+2, rightX);
+ emptyScanline = false;
}
}
+ previous = edge;
+ if (edge.isClip)
+ insideClip = ! insideClip;
else
- {
- // TODO: Use integer arithmetics here.
- if (edge.y1 > (y + (subY / (double) AA_SAMPLING)))
- {
- //System.err.println(edge);
- previous = edge;
- active = true;
- }
- }
+ insideShape = ! insideShape;
}
yindex++;
}
firstSubline = 0;
// Render full scanline.
//System.err.println("scanline: " + y);
- fillScanlineAA(alpha, (int) minX, (int) y, numScanlinePixels, pCtx);
+ if (! emptyScanline)
+ fillScanlineAA(alpha, leftX, (int) y, rightX - leftX, pCtx,
+ (int) minX);
}
- if (paint instanceof Color && composite == AlphaComposite.SrcOver)
- rawSetForeground((Color) paint);
pCtx.dispose();
}
@@ -1748,40 +1979,54 @@ public abstract class AbstractGraphics2D
* @param x0 the beginning of the scanline
* @param y the y coordinate of the line
*/
- private void fillScanlineAA(int[] alpha, int x0, int y, int numScanlinePixels,
- PaintContext pCtx)
+ private void fillScanlineAA(int[] alpha, int x0, int yy, int numPixels,
+ PaintContext pCtx, int offs)
{
- // FIXME: This doesn't work. Fixit.
CompositeContext cCtx = composite.createContext(pCtx.getColorModel(),
getColorModel(),
renderingHints);
- Raster paintRaster = pCtx.getRaster(x0, y, numScanlinePixels, 1);
- System.err.println("paintColorModel: " + pCtx.getColorModel());
+ Raster paintRaster = pCtx.getRaster(x0, yy, numPixels, 1);
+ //System.err.println("paintColorModel: " + pCtx.getColorModel());
WritableRaster aaRaster = paintRaster.createCompatibleWritableRaster();
int numBands = paintRaster.getNumBands();
- int[] pixels = new int[numScanlinePixels + paintRaster.getNumBands()];
- pixels = paintRaster.getPixels(x0, y, numScanlinePixels, 1, pixels);
ColorModel cm = pCtx.getColorModel();
-
double lastAlpha = 0.;
int lastAlphaInt = 0;
- int[] components = new int[4];
-
- for (int i = 0; i < pixels.length; i++)
+
+ Object pixel = null;
+ int[] comps = null;
+ int x1 = x0 + numPixels;
+ for (int x = x0; x < x1; x++)
{
+ int i = x - offs;
if (alpha[i] != 0)
{
lastAlphaInt += alpha[i];
- lastAlpha = lastAlphaInt / AA_SAMPLING;
+ lastAlpha = (double) lastAlphaInt / (double) AA_SAMPLING;
+ alpha[i] = 0;
}
- components = cm.getComponents(pixel[i], components, 0);
- components[0] = (int) (components[0] * lastAlpha);
- pixel[i] = cm.getDataElement(components, 0);
+ pixel = paintRaster.getDataElements(x - x0, 0, pixel);
+ comps = cm.getComponents(pixel, comps, 0);
+ if (cm.hasAlpha() && ! cm.isAlphaPremultiplied())
+ comps[comps.length - 1] *= lastAlpha;
+ else
+ {
+ int max;
+ if (cm.hasAlpha())
+ max = comps.length - 2;
+ else
+ max = comps.length - 1;
+ for (int j = 0; j < max; j++)
+ comps[j] *= lastAlpha;
+ }
+ pixel = cm.getDataElements(comps, 0, pixel);
+ aaRaster.setDataElements(x - x0, 0, pixel);
}
- aaRaster.setPixels(0, 0, numScanlinePixels, 1, pixels);
- cCtx.compose(aaRaster, destinationRaster, destinationRaster);
- updateRaster(destinationRaster, x0, y, numScanlinePixels, 1);
+ WritableRaster targetChild =
+ destinationRaster.createWritableTranslatedChild(-x0, -yy);
+ cCtx.compose(aaRaster, targetChild, targetChild);
+ updateRaster(destinationRaster, x0, yy, numPixels, 1);
cCtx.dispose();
}
@@ -1799,8 +2044,8 @@ public abstract class AbstractGraphics2D
// FIXME: Should not be necessary. A clip of null should mean
// 'clip against device bounds.
- clip = getDeviceBounds();
destinationRaster = getDestinationRaster();
+ clip = getDeviceBounds();
}
/**
@@ -1913,40 +2158,77 @@ public abstract class AbstractGraphics2D
}
/**
- * Clips the specified shape using the current clip. If the resulting shape
- * is empty, this will return <code>null</code>.
+ * Converts the specified shape into a list of segments.
*
- * @param s the shape to clip
+ * @param s the shape to convert
+ * @param t the transformation to apply before converting
+ * @param deviceBounds an output parameter; holds the bounding rectangle of
+ * s in device space after return
+ * @param isClip true when the shape is a clip, false for normal shapes;
+ * this influences the settings in the created PolyEdge instances.
*
- * @return the clipped shape or <code>null</code> if the result is empty
+ * @return a list of PolyEdge that form the shape in device space
*/
- private Shape clipShape(Shape s)
+ private ArrayList getSegments(Shape s, AffineTransform t,
+ Rectangle2D deviceBounds, boolean isClip,
+ double offs)
{
- Shape clipped = null;
+ // Flatten the path. TODO: Determine the best flattening factor
+ // wrt to speed and quality.
+ PathIterator path = s.getPathIterator(getTransform(), 1.0);
- // Clip the shape if necessary.
- if (clip != null)
- {
- Area a;
- if (! (s instanceof Area))
- a = new Area(s);
- else
- a = (Area) s;
+ // Build up polygons and let the native backend render this using
+ // rawFillShape() which would provide a default implementation for
+ // drawPixel using a PolyScan algorithm.
+ double[] seg = new double[6];
- Area clipArea;
- if (! (clip instanceof Area))
- clipArea = new Area(clip);
- else
- clipArea = (Area) clip;
+ // TODO: Use ArrayList<PolyEdge> here when availble.
+ ArrayList segs = new ArrayList();
+ double segX = 0.; // The start point of the current edge.
+ double segY = 0.;
+ double polyX = 0.; // The start point of the current polygon.
+ double polyY = 0.;
- a.intersect(clipArea);
- if (! a.isEmpty())
- clipped = a;
- }
- else
+ double minX = Integer.MAX_VALUE;
+ double maxX = Integer.MIN_VALUE;
+ double minY = Integer.MAX_VALUE;
+ double maxY = Integer.MIN_VALUE;
+
+ //System.err.println("fill polygon");
+ while (! path.isDone())
{
- clipped = s;
+ int segType = path.currentSegment(seg);
+ minX = Math.min(minX, seg[0]);
+ maxX = Math.max(maxX, seg[0]);
+ minY = Math.min(minY, seg[1]);
+ maxY = Math.max(maxY, seg[1]);
+
+ //System.err.println("segment: " + segType + ", " + seg[0] + ", " + seg[1]);
+ if (segType == PathIterator.SEG_MOVETO)
+ {
+ segX = seg[0];
+ segY = seg[1];
+ polyX = seg[0];
+ polyY = seg[1];
+ }
+ else if (segType == PathIterator.SEG_CLOSE)
+ {
+ // Close the polyline.
+ PolyEdge edge = new PolyEdge(segX, segY - offs,
+ polyX, polyY - offs, isClip);
+ segs.add(edge);
+ }
+ else if (segType == PathIterator.SEG_LINETO)
+ {
+ PolyEdge edge = new PolyEdge(segX, segY - offs,
+ seg[0], seg[1] - offs, isClip);
+ segs.add(edge);
+ segX = seg[0];
+ segY = seg[1];
+ }
+ path.next();
}
- return clipped;
+ deviceBounds.setRect(minX, minY, maxX - minX, maxY - minY);
+ return segs;
}
}
diff --git a/gnu/java/awt/java2d/AlphaCompositeContext.java b/gnu/java/awt/java2d/AlphaCompositeContext.java
index e67c92148..2e3690d83 100644
--- a/gnu/java/awt/java2d/AlphaCompositeContext.java
+++ b/gnu/java/awt/java2d/AlphaCompositeContext.java
@@ -236,7 +236,7 @@ public class AlphaCompositeContext
}
else
{
- for (int i = srcComponentsLength - 1; i >= 0; i--)
+ for (int i = srcComponentsLength - 2; i >= 0; i--)
srcComponents[i] *= srcComponents[srcComponentsLength - 1];
}
if (! dstColorModel.isAlphaPremultiplied())
diff --git a/gnu/java/awt/java2d/ImagePaint.java b/gnu/java/awt/java2d/ImagePaint.java
new file mode 100644
index 000000000..7e5fb5638
--- /dev/null
+++ b/gnu/java/awt/java2d/ImagePaint.java
@@ -0,0 +1,192 @@
+/* ImagePaint.java -- Supplies the pixels for image rendering
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.awt.java2d;
+
+import java.awt.Paint;
+import java.awt.PaintContext;
+import java.awt.Rectangle;
+import java.awt.RenderingHints;
+import java.awt.Transparency;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.ColorModel;
+import java.awt.image.Raster;
+import java.awt.image.RenderedImage;
+import java.awt.image.WritableRaster;
+
+/**
+ * This class is used as a temporary Paint object to supply the pixel values
+ * for image rendering using the normal scanline conversion implementation.
+ *
+ * @author Roman Kennke (kennke@aicas.com)
+ */
+public class ImagePaint
+ implements Paint
+{
+
+ /**
+ * The PaintContext implementation for the ImagePaint.
+ */
+ private class ImagePaintContext
+ implements PaintContext
+ {
+
+ /**
+ * The target raster.
+ */
+ private WritableRaster target;
+
+ /**
+ * Nothing to do here.
+ */
+ public void dispose()
+ {
+ // Nothing to do here.
+ }
+
+ /**
+ * Returns the color model.
+ *
+ * @return the color model
+ */
+ public ColorModel getColorModel()
+ {
+ return image.getColorModel();
+ }
+
+ /**
+ * Supplies the pixel to be rendered.
+ *
+ * @see PaintContext#getRaster(int, int, int, int)
+ */
+ public Raster getRaster(int x1, int y1, int w, int h)
+ {
+ ensureRasterSize(w, h);
+ int x2 = x1 + w;
+ int y2 = y1 + h;
+ float[] src = new float[2];
+ float[] dest = new float[2];
+ Raster source = image.getData();
+ int minX = source.getMinX();
+ int maxX = source.getWidth() + minX;
+ int minY = source.getMinY();
+ int maxY = source.getHeight() + minY;
+ Object pixel = null;
+ for (int y = y1; y < y2; y++)
+ {
+ for (int x = x1; x < x2; x++)
+ {
+ src[0] = x;
+ src[1] = y;
+ transform.transform(src, 0, dest, 0, 1);
+ int dx = (int) dest[0];
+ int dy = (int) dest[1];
+ // Pixels outside the source image are not of interest, skip
+ // them.
+ if (dx >= minX && dx < maxX && dy >= minY && dy < maxY)
+ {
+ pixel = source.getDataElements(dx, dy, pixel);
+ target.setDataElements(x - x1, y - y1, pixel);
+ }
+ }
+ }
+ return target;
+ }
+
+ /**
+ * Ensures that the target raster exists and has at least the specified
+ * size.
+ *
+ * @param w the requested target width
+ * @param h the requested target height
+ */
+ private void ensureRasterSize(int w, int h)
+ {
+ if (target == null || target.getWidth() < w || target.getHeight() < h)
+ {
+ Raster s = image.getData();
+ target = s.createCompatibleWritableRaster(w, h);
+ }
+ }
+ }
+
+ /**
+ * The image to render.
+ */
+ RenderedImage image;
+
+ /**
+ * The transform from image space to device space. This is the inversed
+ * transform of the concatenated
+ * transform image space -> user space -> device space transform.
+ */
+ AffineTransform transform;
+
+ /**
+ * Creates a new ImagePaint for rendering the specified image using the
+ * specified device space -> image space transform. This transform
+ * is the inversed transform of the usual image space -> user space -> device
+ * space transform.
+ *
+ * The ImagePaint will only render the image in the specified area of
+ * interest (which is specified in image space).
+ *
+ * @param i the image to render
+ * @param t the device space to user space transform
+ */
+ ImagePaint(RenderedImage i, AffineTransform t)
+ {
+ image = i;
+ transform = t;
+ }
+
+ public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
+ Rectangle2D userBounds,
+ AffineTransform xform,
+ RenderingHints hints)
+ {
+ return new ImagePaintContext();
+ }
+
+ public int getTransparency()
+ {
+ return Transparency.OPAQUE;
+ }
+
+}
diff --git a/gnu/java/awt/java2d/PolyEdge.java b/gnu/java/awt/java2d/PolyEdge.java
index 621bd3ad8..8dbdbabcb 100644
--- a/gnu/java/awt/java2d/PolyEdge.java
+++ b/gnu/java/awt/java2d/PolyEdge.java
@@ -65,6 +65,11 @@ public class PolyEdge
double xIntersection;
/**
+ * Indicates whether this edge is from the clip or from the target shape.
+ */
+ boolean isClip;
+
+ /**
* Creates a new PolyEdge with the specified coordinates.
*
* @param x0 the starting point, x coordinate
@@ -72,8 +77,9 @@ public class PolyEdge
* @param x1 the end point, x coordinate
* @param y1 the end point, y coordinate
*/
- PolyEdge(double x0, double y0, double x1, double y1)
+ PolyEdge(double x0, double y0, double x1, double y1, boolean clip)
{
+ isClip = clip;
if (y0 < y1)
{
this.x0 = x0;
diff --git a/gnu/java/awt/java2d/RasterGraphics.java b/gnu/java/awt/java2d/RasterGraphics.java
new file mode 100644
index 000000000..98d47b406
--- /dev/null
+++ b/gnu/java/awt/java2d/RasterGraphics.java
@@ -0,0 +1,103 @@
+/* RasterGraphics.java -- A Graphics2D impl for Rasters
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.awt.java2d;
+
+import java.awt.GraphicsConfiguration;
+import java.awt.image.ColorModel;
+import java.awt.image.WritableRaster;
+
+/**
+ * A Graphics2D implementation that operates on Raster objects. This is
+ * primarily used for BufferedImages, but can theoretically be used on
+ * arbitrary WritableRasters.
+ *
+ * @author Roman Kennke (kennke@aicas.com)
+ */
+public class RasterGraphics
+ extends AbstractGraphics2D
+{
+
+ /**
+ * The raster on which we operate.
+ */
+ private WritableRaster raster;
+
+ /**
+ * The color model of this Graphics instance.
+ */
+ private ColorModel colorModel;
+
+ public RasterGraphics(WritableRaster r, ColorModel cm)
+ {
+ super();
+ raster = r;
+ colorModel = cm;
+ init();
+ }
+
+ /**
+ * Returns the color model of this Graphics object.
+ *
+ * @return the color model of this Graphics object
+ */
+ protected ColorModel getColorModel()
+ {
+ return colorModel;
+ }
+
+ /**
+ * Returns a WritableRaster that is used by this class to perform the
+ * rendering in. It is not necessary that the target surface immediately
+ * reflects changes in the raster. Updates to the raster are notified via
+ * {@link AbstractGraphics2D#updateRaster}.
+ *
+ * @return the destination raster
+ */
+ protected WritableRaster getDestinationRaster()
+ {
+ return raster;
+ }
+
+ public GraphicsConfiguration getDeviceConfiguration()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
diff --git a/gnu/java/awt/peer/gtk/GdkGraphics.java b/gnu/java/awt/peer/gtk/GdkGraphics.java
index 3c3cbdf32..a5b9ff135 100644
--- a/gnu/java/awt/peer/gtk/GdkGraphics.java
+++ b/gnu/java/awt/peer/gtk/GdkGraphics.java
@@ -39,6 +39,7 @@ exception statement from your version. */
package gnu.java.awt.peer.gtk;
import gnu.classpath.Configuration;
+import gnu.java.awt.AWTUtilities;
import java.awt.Color;
import java.awt.Dimension;
@@ -160,7 +161,7 @@ public class GdkGraphics extends Graphics
if (component != null && ! component.isRealized ())
return;
- clip = clip.intersection (new Rectangle (x, y, width, height));
+ computeIntersection(x, y, width, height, clip);
setClipRectangle (clip.x, clip.y, clip.width, clip.height);
}
@@ -493,4 +494,26 @@ public class GdkGraphics extends Graphics
component = g.component;
nativeCopyState(g);
}
+
+ private Rectangle computeIntersection(int x, int y, int w, int h,
+ Rectangle rect)
+ {
+ int x2 = (int) rect.x;
+ int y2 = (int) rect.y;
+ int w2 = (int) rect.width;
+ int h2 = (int) rect.height;
+
+ int dx = (x > x2) ? x : x2;
+ int dy = (y > y2) ? y : y2;
+ int dw = (x + w < x2 + w2) ? (x + w - dx) : (x2 + w2 - dx);
+ int dh = (y + h < y2 + h2) ? (y + h - dy) : (y2 + h2 - dy);
+
+ if (dw >= 0 && dh >= 0)
+ rect.setBounds(dx, dy, dw, dh);
+ else
+ rect.setBounds(0, 0, 0, 0);
+
+ return rect;
+ }
+
}
diff --git a/gnu/java/awt/peer/swing/SwingComponent.java b/gnu/java/awt/peer/swing/SwingComponent.java
index 04ca7294f..a51b758ad 100644
--- a/gnu/java/awt/peer/swing/SwingComponent.java
+++ b/gnu/java/awt/peer/swing/SwingComponent.java
@@ -62,7 +62,7 @@ public interface SwingComponent
/**
* Handles a mouse event. This is usually forwarded to
- * {@link Component#processMouseMotionEvent(MouseEvent)} of the swing
+ * {@link java.awt.Component#processMouseMotionEvent(MouseEvent)} of the swing
* component.
*
* @param ev the mouse event
@@ -71,7 +71,7 @@ public interface SwingComponent
/**
* Handles a mouse motion event. This is usually forwarded to
- * {@link Component#processMouseEvent(MouseEvent)} of the swing
+ * {@link java.awt.Component#processMouseEvent(MouseEvent)} of the swing
* component.
*
* @param ev the mouse motion event
@@ -80,7 +80,7 @@ public interface SwingComponent
/**
* Handles a key event. This is usually forwarded to
- * {@link Component#processKeyEvent(KeyEvent)} of the swing
+ * {@link java.awt.Component#processKeyEvent(KeyEvent)} of the swing
* component.
*
* @param ev the key event
diff --git a/gnu/java/awt/peer/swing/SwingComponentPeer.java b/gnu/java/awt/peer/swing/SwingComponentPeer.java
index 5d484e021..f60c8e96c 100644
--- a/gnu/java/awt/peer/swing/SwingComponentPeer.java
+++ b/gnu/java/awt/peer/swing/SwingComponentPeer.java
@@ -48,6 +48,8 @@ import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
@@ -98,8 +100,9 @@ public class SwingComponentPeer
/**
* Creates a SwingComponentPeer instance. Subclasses are expected to call
- * this constructor and thereafter call {@link #init(Component, JComponent)}
- * in order to setup the AWT and Swing components properly.
+ * this constructor and thereafter call
+ * {@link #init(Component, SwingComponent)} in order to setup the AWT and
+ * Swing components properly.
*/
protected SwingComponentPeer()
{
@@ -164,9 +167,12 @@ public class SwingComponentPeer
*/
public Image createImage(int width, int height)
{
- Component parent = awtComponent.getParent();
- ComponentPeer parentPeer = parent.getPeer();
- return parentPeer.createImage(width, height);
+ GraphicsEnvironment graphicsEnv =
+ GraphicsEnvironment.getLocalGraphicsEnvironment();
+ GraphicsDevice dev = graphicsEnv.getDefaultScreenDevice();
+ GraphicsConfiguration conf = dev.getDefaultConfiguration();
+ Image image = conf.createCompatibleImage(width, height);
+ return image;
}
/**
@@ -442,20 +448,6 @@ public class SwingComponentPeer
return retVal;
}
- /**
- * Prepares an image for rendering on this component. This is called by
- * {@link Component#prepareImage(Image, int, int, ImageObserver)}.
- *
- * @param img the image to prepare
- * @param width the desired width of the rendered image
- * @param height the desired height of the rendered image
- * @param ob the image observer to be notified of updates in the preparation
- * process
- *
- * @return <code>true</code> if the image has been fully prepared,
- * <code>false</code> otherwise (in which case the image observer
- * receives updates)
- */
public void paint(Graphics graphics)
{
// FIXME: I don't know what this method is supposed to do.
@@ -478,8 +470,17 @@ public class SwingComponentPeer
public boolean prepareImage(Image img, int width, int height, ImageObserver ob)
{
Component parent = awtComponent.getParent();
- ComponentPeer parentPeer = parent.getPeer();
- return parentPeer.prepareImage(img, width, height, ob);
+ boolean res;
+ if(parent != null)
+ {
+ ComponentPeer parentPeer = parent.getPeer();
+ res = parentPeer.prepareImage(img, width, height, ob);
+ }
+ else
+ {
+ res = Toolkit.getDefaultToolkit().prepareImage(img, width, height, ob);
+ }
+ return res;
}
public void print(Graphics graphics)
diff --git a/gnu/java/awt/peer/swing/SwingContainerPeer.java b/gnu/java/awt/peer/swing/SwingContainerPeer.java
index 0b2fb992f..f433e1b5c 100644
--- a/gnu/java/awt/peer/swing/SwingContainerPeer.java
+++ b/gnu/java/awt/peer/swing/SwingContainerPeer.java
@@ -92,7 +92,12 @@ public class SwingContainerPeer
*/
public Insets getInsets()
{
- return insets();
+ Insets retVal;
+ if (swingComponent != null)
+ retVal = swingComponent.getJComponent().getInsets();
+ else
+ retVal = new Insets(0, 0, 0, 0);
+ return retVal;
}
/**
@@ -209,6 +214,8 @@ public class SwingContainerPeer
protected void handleMouseEvent(MouseEvent ev)
{
Component comp = awtComponent.getComponentAt(ev.getPoint());
+ if(comp == null)
+ comp = awtComponent;
if (comp != null)
{
ComponentPeer peer = comp.getPeer();
diff --git a/gnu/java/awt/peer/swing/SwingFramePeer.java b/gnu/java/awt/peer/swing/SwingFramePeer.java
index fea1b504a..0d5a02d78 100644
--- a/gnu/java/awt/peer/swing/SwingFramePeer.java
+++ b/gnu/java/awt/peer/swing/SwingFramePeer.java
@@ -53,9 +53,9 @@ import java.awt.peer.FramePeer;
* As a minimum, a subclass must implement all the remaining abstract methods
* as well as the following methods:
* <ul>
- * <li>{@link ComponentPeer#getLocationOnScreen()}</li>
- * <li>{@link ComponentPeer#getGraphics()}</li>
- * <li>{@link ComponentPeer#createImage(int, int)}</li>
+ * <li>{@link java.awt.peer.ComponentPeer#getLocationOnScreen()}</li>
+ * <li>{@link java.awt.peer.ComponentPeer#getGraphics()}</li>
+ * <li>{@link java.awt.peer.ComponentPeer#createImage(int, int)}</li>
* </ul>
*
* @author Roman Kennke (kennke@aicas.com)
diff --git a/gnu/java/awt/peer/swing/SwingMenuBarPeer.java b/gnu/java/awt/peer/swing/SwingMenuBarPeer.java
index bd9dcd77a..0033efb02 100644
--- a/gnu/java/awt/peer/swing/SwingMenuBarPeer.java
+++ b/gnu/java/awt/peer/swing/SwingMenuBarPeer.java
@@ -174,7 +174,7 @@ public class SwingMenuBarPeer
/**
* Adds a help menu to the menu bar.
*
- * @param m the menu to add
+ * @param menu the menu to add
*/
public void addHelpMenu(Menu menu)
{
diff --git a/gnu/java/awt/peer/swing/SwingTextFieldPeer.java b/gnu/java/awt/peer/swing/SwingTextFieldPeer.java
index a4c6d82d2..0c3b4e726 100644
--- a/gnu/java/awt/peer/swing/SwingTextFieldPeer.java
+++ b/gnu/java/awt/peer/swing/SwingTextFieldPeer.java
@@ -283,7 +283,7 @@ public class SwingTextFieldPeer
* @param startPos the start index of the selection
* @param endPos the start index of the selection
*/
- public void select(int start_pos, int endPos)
+ public void select(int startPos, int endPos)
{
// TODO: Must be implemented.
}
diff --git a/gnu/java/awt/peer/swing/SwingWindowPeer.java b/gnu/java/awt/peer/swing/SwingWindowPeer.java
index 2f89795ca..43a509b95 100644
--- a/gnu/java/awt/peer/swing/SwingWindowPeer.java
+++ b/gnu/java/awt/peer/swing/SwingWindowPeer.java
@@ -48,9 +48,9 @@ import java.awt.peer.WindowPeer;
* As a minimum, a subclass must implement all the remaining abstract methods
* as well as the following methods:
* <ul>
- * <li>{@link ComponentPeer#getLocationOnScreen()}</li>
- * <li>{@link ComponentPeer#getGraphics()}</li>
- * <li>{@link ComponentPeer#createImage(int, int)}</li>
+ * <li>{@link java.awt.peer.ComponentPeer#getLocationOnScreen()}</li>
+ * <li>{@link java.awt.peer.ComponentPeer#getGraphics()}</li>
+ * <li>{@link java.awt.peer.ComponentPeer#createImage(int, int)}</li>
* </ul>
*
* @author Roman Kennke (kennke@aicas.com)
diff --git a/gnu/java/awt/print/JavaPrinterGraphics.java b/gnu/java/awt/print/JavaPrinterGraphics.java
new file mode 100644
index 000000000..af31309ed
--- /dev/null
+++ b/gnu/java/awt/print/JavaPrinterGraphics.java
@@ -0,0 +1,518 @@
+/* JavaPrinterGraphics.java -- AWT printer rendering class.
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.java.awt.print;
+
+import gnu.java.awt.peer.gtk.GtkImage;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.Rectangle;
+import java.awt.Shape;
+import java.awt.geom.AffineTransform;
+import java.awt.image.ImageObserver;
+import java.awt.image.PixelGrabber;
+import java.awt.print.PageFormat;
+import java.awt.print.Pageable;
+import java.awt.print.Paper;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterGraphics;
+import java.awt.print.PrinterJob;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.text.AttributedCharacterIterator;
+
+/**
+ * Graphics context to draw to PostScript.
+ *
+ * @author Sven de Marothy
+ */
+public class JavaPrinterGraphics extends Graphics implements PrinterGraphics
+{
+
+ /**
+ * The used graphics context.
+ */
+ private Graphics g;
+
+ /**
+ * The associated printer job.
+ */
+ private PrinterJob printerJob;
+
+ /**
+ * Rendering resolution
+ */
+ private static final double DPI = 72.0;
+
+ /**
+ * Rendered image size.
+ */
+ private int xSize, ySize;
+
+ /**
+ * The image to render to.
+ */
+ private Image image;
+
+ public JavaPrinterGraphics( PrinterJob printerJob )
+ {
+ this.printerJob = printerJob;
+ }
+
+ /**
+ * Spool a document to PostScript.
+ * If Pageable is non-null, it will print that, otherwise it will use
+ * the supplied printable and pageFormat.
+ */
+ public SpooledDocument spoolPostScript(Printable printable,
+ PageFormat pageFormat,
+ Pageable pageable)
+ throws PrinterException
+ {
+ try
+ {
+ // spool to a temporary file
+ File temp = File.createTempFile("cpspool", ".ps");
+ temp.deleteOnExit();
+
+ PrintWriter out = new PrintWriter
+ (new BufferedWriter
+ (new OutputStreamWriter
+ (new FileOutputStream(temp), "ISO8859_1"), 1000000));
+
+ writePSHeader(out);
+
+ if(pageable != null)
+ {
+ for(int index = 0; index < pageable.getNumberOfPages(); index++)
+ spoolPage(out, pageable.getPrintable(index),
+ pageable.getPageFormat(index), index);
+ }
+ else
+ {
+ int index = 0;
+ while(spoolPage(out, printable, pageFormat, index++) ==
+ Printable.PAGE_EXISTS);
+ }
+ out.println("%%Trailer");
+ out.println("%%EOF");
+ out.close();
+ return new SpooledDocument( temp );
+ }
+ catch (IOException e)
+ {
+ PrinterException pe = new PrinterException();
+ pe.initCause(e);
+ throw pe;
+ }
+ }
+
+ /**
+ * Spools a single page, returns NO_SUCH_PAGE unsuccessful,
+ * PAGE_EXISTS if it was.
+ */
+ public int spoolPage(PrintWriter out,
+ Printable printable,
+ PageFormat pageFormat,
+ int index) throws IOException, PrinterException
+ {
+ initImage( pageFormat );
+ if(printable.print(this, pageFormat, index) == Printable.NO_SUCH_PAGE)
+ return Printable.NO_SUCH_PAGE;
+ g.dispose();
+ g = null;
+ writePage( out, pageFormat );
+ return Printable.PAGE_EXISTS;
+ }
+
+ private void initImage(PageFormat pageFormat)
+ {
+ // Create a really big image and draw to that.
+ xSize = (int)(DPI*pageFormat.getWidth()/72.0);
+ ySize = (int)(DPI*pageFormat.getHeight()/72.0);
+
+ // Swap X and Y sizes if it's a Landscape page.
+ if( pageFormat.getOrientation() != PageFormat.PORTRAIT )
+ {
+ int t = xSize;
+ xSize = ySize;
+ ySize = t;
+ }
+
+ // FIXME: This should at least be BufferedImage.
+ // Fix once we have a working B.I.
+ // Graphics2D should also be supported of course.
+ image = new GtkImage(xSize, ySize);
+
+ g = image.getGraphics();
+ setColor(Color.white);
+ fillRect(0, 0, xSize, ySize);
+ setColor(Color.black);
+ }
+
+ private void writePSHeader(PrintWriter out)
+ {
+ out.println("%!PS-Adobe-3.0");
+ out.println("%%Title: "+printerJob.getJobName());
+ out.println("%%Creator: GNU Classpath ");
+ out.println("%%DocumentData: Clean8Bit");
+
+ out.println("%%DocumentNeededResources: font Times-Roman Helvetica Courier");
+ // out.println("%%Pages: "+); // FIXME # pages.
+ out.println("%%EndComments");
+
+ out.println("%%BeginProlog");
+ out.println("%%EndProlog");
+ out.println("%%BeginSetup");
+
+ // FIXME: Paper name
+ // E.g. "A4" "Letter"
+ // out.println("%%BeginFeature: *PageSize A4");
+
+ out.println("%%EndFeature");
+
+ out.println("%%EndSetup");
+
+ // out.println("%%Page: 1 1");
+ }
+
+ private void writePage(PrintWriter out, PageFormat pageFormat)
+ {
+ out.println("%%BeginPageSetup");
+
+ Paper p = pageFormat.getPaper();
+ double pWidth = p.getWidth();
+ double pHeight = p.getHeight();
+
+ if( pageFormat.getOrientation() == PageFormat.PORTRAIT )
+ out.println( "%%Orientation: Portrait" );
+ else
+ {
+ out.println( "%%Orientation: Landscape" );
+ double t = pWidth;
+ pWidth = pHeight;
+ pHeight = t;
+ }
+
+ out.println("gsave % first save");
+
+ // 595x842; 612x792 respectively
+ out.println("<< /PageSize [" +pWidth + " "+pHeight+ "] >> setpagedevice");
+
+ // invert the Y axis so that we get screen-like coordinates instead.
+ AffineTransform pageTransform = new AffineTransform();
+ if( pageFormat.getOrientation() == PageFormat.REVERSE_LANDSCAPE )
+ {
+ pageTransform.translate(pWidth, pHeight);
+ pageTransform.scale(-1.0, -1.0);
+ }
+ concatCTM(out, pageTransform);
+ out.println("%%EndPageSetup");
+
+ out.println("gsave");
+
+
+ // Draw the image
+ out.println(xSize+" "+ySize+" 8 [1 0 0 -1 0 "+ySize+" ]");
+ out.println("{currentfile 3 string readhexstring pop} bind");
+ out.println("false 3 colorimage");
+ int[] pixels = new int[xSize * ySize];
+ PixelGrabber pg = new PixelGrabber(image, 0, 0, xSize, ySize, pixels, 0, xSize);
+
+ try {
+ pg.grabPixels();
+ } catch (InterruptedException e) {
+ out.println("% Bug getting pixels!");
+ }
+
+ int n = 0;
+ for (int j = 0; j < ySize; j++) {
+ for (int i = 0; i < xSize; i++) {
+ out.print( colorTripleHex(pixels[j * xSize + i]) );
+ if(((++n)%11) == 0) out.println();
+ }
+ }
+
+ out.println();
+ out.println("%%EOF");
+ out.println("grestore");
+ out.println("showpage");
+ }
+
+ /**
+ * Get a nonsperated hex RGB triple, e.g. FFFFFF = white
+ */
+ private String colorTripleHex(int num){
+ String s = "";
+
+ try {
+ s = Integer.toHexString( ( num & 0x00FFFFFF ) );
+ if( s.length() < 6 )
+ {
+ s = "000000"+s;
+ return s.substring(s.length()-6);
+ }
+ } catch (Exception e){
+ s = "FFFFFF";
+ }
+
+ return s;
+ }
+
+ private void concatCTM(PrintWriter out, AffineTransform Tx){
+ double[] matrixElements = new double[6];
+ Tx.getMatrix(matrixElements);
+
+ out.print("[ ");
+ for(int i=0;i<6;i++)
+ out.print(matrixElements[i]+" ");
+ out.println("] concat");
+ }
+
+ //-----------------------------------------------------------------------------
+ /**
+ * PrinterGraphics method - Returns the printer job associated with this object.
+ */
+ public PrinterJob getPrinterJob()
+ {
+ return printerJob;
+ }
+
+ /**
+ * The rest of the methods here are just pass-throughs to g.
+ */
+ public void clearRect(int x, int y, int width, int height)
+ {
+ g.clearRect(x, y, width, height);
+ }
+
+ public void clipRect(int x, int y, int width, int height)
+ {
+ g.clipRect(x, y, width, height);
+ }
+
+ public void copyArea(int x, int y, int width, int height, int dx, int dy)
+ {
+ g.copyArea(x, y, width, height, dx, dy);
+ }
+
+ public Graphics create()
+ {
+ return g.create();
+ }
+
+ public void dispose()
+ {
+ }
+
+ public void drawArc(int x, int y, int width, int height, int startAngle,
+ int arcAngle)
+ {
+ g.drawArc(x, y, width, height, startAngle, arcAngle);
+ }
+
+ public boolean drawImage(Image img, int x, int y, Color bgcolor,
+ ImageObserver observer)
+ {
+ return g.drawImage(img, x, y, bgcolor, observer);
+ }
+
+ public boolean drawImage(Image img, int x, int y, ImageObserver observer)
+ {
+ return g.drawImage(img, x, y, observer);
+ }
+
+ public boolean drawImage(Image img, int x, int y, int width, int height,
+ Color bgcolor, ImageObserver observer)
+ {
+ return g.drawImage(img, x, y, width, height, bgcolor, observer);
+ }
+
+ public boolean drawImage(Image img, int x, int y, int width, int height,
+ ImageObserver observer)
+ {
+ return g.drawImage(img, x, y, width, height, observer);
+ }
+
+ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
+ int sx1, int sy1, int sx2, int sy2, Color bgcolor,
+ ImageObserver observer)
+ {
+ return g.drawImage(img, dx1, dy1, dx2, dy2,
+ sx1, sy1, sx2, sy2, bgcolor, observer);
+ }
+
+ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
+ int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
+ {
+ return g.drawImage(img, dx1, dy1, dx2, dy2,
+ sx1, sy1, sx2, sy2, observer);
+ }
+
+ public void drawLine(int x1, int y1, int x2, int y2)
+ {
+ g.drawLine(x1, y1, x2, y2);
+ }
+
+ public void drawOval(int x, int y, int width, int height)
+ {
+ g.drawOval(x, y, width, height);
+ }
+
+ public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
+ {
+ g.drawPolygon(xPoints, yPoints, nPoints);
+ }
+
+ public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
+ {
+ g.drawPolyline(xPoints, yPoints, nPoints);
+ }
+
+ public void drawRoundRect(int x, int y, int width, int height,
+ int arcWidth, int arcHeight)
+ {
+ g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
+ }
+
+ public void drawString(AttributedCharacterIterator iterator, int x, int y)
+ {
+ g.drawString(iterator, x, y);
+ }
+
+ public void drawString(String str, int x, int y)
+ {
+ g.drawString(str, x, y);
+ }
+
+ public void fillArc(int x, int y, int width, int height,
+ int startAngle, int arcAngle)
+ {
+ g.fillArc(x, y, width, height, startAngle, arcAngle);
+ }
+
+ public void fillOval(int x, int y, int width, int height)
+ {
+ g.fillOval(x, y, width, height);
+ }
+
+ public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
+ {
+ g.fillPolygon(xPoints, yPoints, nPoints);
+ }
+
+ public void fillRect(int x, int y, int width, int height)
+ {
+ g.fillRect(x, y, width, height);
+ }
+
+ public void fillRoundRect(int x, int y, int width, int height,
+ int arcWidth, int arcHeight)
+ {
+ g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
+ }
+
+ public Shape getClip()
+ {
+ return g.getClip();
+ }
+
+ public Rectangle getClipBounds()
+ {
+ return g.getClipBounds();
+ }
+
+ public Color getColor()
+ {
+ return g.getColor();
+ }
+
+ public Font getFont()
+ {
+ return g.getFont();
+ }
+
+ public FontMetrics getFontMetrics(Font f)
+ {
+ return g.getFontMetrics(f);
+ }
+
+ public void setClip(int x, int y, int width, int height)
+ {
+ g.setClip(x, y, width, height);
+ }
+
+ public void setClip(Shape clip)
+ {
+ g.setClip(clip);
+ }
+
+ public void setColor(Color c)
+ {
+ g.setColor(c);
+ }
+
+ public void setFont(Font font)
+ {
+ g.setFont(font);
+ }
+
+ public void setPaintMode()
+ {
+ g.setPaintMode();
+ }
+
+ public void setXORMode(Color c1)
+ {
+ g.setXORMode(c1);
+ }
+
+ public void translate(int x, int y)
+ {
+ g.translate(x, y);
+ }
+}
+
diff --git a/gnu/java/awt/print/JavaPrinterJob.java b/gnu/java/awt/print/JavaPrinterJob.java
new file mode 100644
index 000000000..adeeba04a
--- /dev/null
+++ b/gnu/java/awt/print/JavaPrinterJob.java
@@ -0,0 +1,403 @@
+/* JavaPrinterJob.java -- AWT printing implemented on javax.print.
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.awt.print;
+
+import java.awt.HeadlessException;
+import java.awt.print.PageFormat;
+import java.awt.print.Pageable;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
+import java.util.Locale;
+
+import javax.print.CancelablePrintJob;
+import javax.print.DocFlavor;
+import javax.print.DocPrintJob;
+import javax.print.PrintException;
+import javax.print.PrintService;
+import javax.print.PrintServiceLookup;
+import javax.print.ServiceUI;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+import javax.print.attribute.IntegerSyntax;
+import javax.print.attribute.PrintRequestAttributeSet;
+import javax.print.attribute.TextSyntax;
+import javax.print.attribute.standard.Copies;
+import javax.print.attribute.standard.JobName;
+import javax.print.attribute.standard.OrientationRequested;
+import javax.print.attribute.standard.RequestingUserName;
+
+/**
+ * This is the default implementation of PrinterJob
+ *
+ * @author Sven de Marothy
+ */
+public class JavaPrinterJob extends PrinterJob
+{
+ /**
+ * The print service associated with this job
+ */
+ private PrintService printer = null;
+
+ /**
+ * Printing options;
+ */
+ private PrintRequestAttributeSet attributes;
+
+ /**
+ * Available print services
+ */
+ private static PrintService[] services;
+
+ /**
+ * The actual print job.
+ */
+ private DocPrintJob printJob;
+
+ /**
+ * The Printable object to print.
+ */
+ private Printable printable;
+
+ /**
+ * Page format.
+ */
+ private PageFormat pageFormat;
+
+ /**
+ * A pageable, or null
+ */
+ private Pageable pageable = null;
+
+ /**
+ * Cancelled or not
+ */
+ private boolean cancelled = false;
+
+ static
+ {
+ // lookup all services without any constraints
+ services = PrintServiceLookup.lookupPrintServices
+ (DocFlavor.INPUT_STREAM.POSTSCRIPT, null);
+ }
+
+ private static final Class copyClass = (new Copies(1)).getClass();
+ private static final Class jobNameClass = (new JobName("", null)).getClass();
+ private static final Class userNameClass = (new RequestingUserName("", null)).getClass();
+
+ /**
+ * Initializes a new instance of <code>PrinterJob</code>.
+ */
+ public JavaPrinterJob()
+ {
+ attributes = new HashPrintRequestAttributeSet();
+ setCopies(1);
+ setJobName("Java Printing");
+ pageFormat = new PageFormat(); // default page format.
+ }
+
+ private void getPageAttributes()
+ {
+ OrientationRequested orientation = (OrientationRequested)
+ attributes.get( OrientationRequested.LANDSCAPE.getCategory() );
+ if( orientation == null)
+ return;
+
+ if( orientation.equals(OrientationRequested.PORTRAIT) )
+ pageFormat.setOrientation(PageFormat.PORTRAIT);
+ else if( orientation.equals(OrientationRequested.LANDSCAPE) )
+ pageFormat.setOrientation(PageFormat.LANDSCAPE);
+ else if( orientation.equals(OrientationRequested.REVERSE_LANDSCAPE) )
+ pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
+ }
+
+ /**
+ * Returns the number of copies to be printed.
+ *
+ * @return The number of copies to be printed.
+ */
+ public int getCopies()
+ {
+ return ((IntegerSyntax)attributes.get( jobNameClass )).getValue();
+ }
+
+ /**
+ * Sets the number of copies to be printed.
+ *
+ * @param copies The number of copies to be printed.
+ */
+ public void setCopies(int copies)
+ {
+ attributes.add( new Copies( copies ) );
+ }
+
+ /**
+ * Returns the name of the print job.
+ *
+ * @return The name of the print job.
+ */
+ public String getJobName()
+ {
+ return ((TextSyntax)attributes.get( jobNameClass )).getValue();
+ }
+
+ /**
+ * Sets the name of the print job.
+ *
+ * @param job_name The name of the print job.
+ */
+ public void setJobName(String job_name)
+ {
+ attributes.add( new JobName(job_name, Locale.getDefault()) );
+ }
+
+ /**
+ * Returns the printing user name.
+ *
+ * @return The printing username.
+ */
+ public String getUserName()
+ {
+ return ((TextSyntax)attributes.get( userNameClass )).getValue();
+ }
+
+ /**
+ * Cancels an in progress print job.
+ */
+ public void cancel()
+ {
+ try
+ {
+ if(printJob != null && (printJob instanceof CancelablePrintJob))
+ {
+ ((CancelablePrintJob)printJob).cancel();
+ cancelled = true;
+ }
+ }
+ catch(PrintException pe)
+ {
+ }
+ }
+
+ /**
+ * Tests whether or not this job has been cancelled.
+ *
+ * @return <code>true</code> if this job has been cancelled, <code>false</code>
+ * otherwise.
+ */
+ public boolean isCancelled()
+ {
+ return cancelled;
+ }
+
+ /**
+ * Clones the specified <code>PageFormat</code> object then alters the
+ * clone so that it represents the default page format.
+ *
+ * @param page_format The <code>PageFormat</code> to clone.
+ *
+ * @return A new default page format.
+ */
+ public PageFormat defaultPage(PageFormat page_format)
+ {
+ return new PageFormat();
+ }
+
+ /**
+ * Displays a dialog box to the user which allows the page format
+ * attributes to be modified.
+ *
+ * @param page_format The <code>PageFormat</code> object to modify.
+ *
+ * @return The modified <code>PageFormat</code>.
+ */
+ public PageFormat pageDialog(PageFormat page_format)
+ throws HeadlessException
+ {
+ return defaultPage(null);
+ }
+
+ /**
+ * Prints the pages.
+ */
+ public void print() throws PrinterException
+ {
+ if( printable == null && pageable == null ) // nothing to print?
+ return;
+
+ PostScriptGraphics2D pg = new PostScriptGraphics2D( this );
+ SpooledDocument doc = pg.spoolPostScript( printable, pageFormat,
+ pageable );
+
+ cancelled = false;
+ printJob = printer.createPrintJob();
+ try
+ {
+ printJob.print(doc, attributes);
+ }
+ catch (PrintException pe)
+ {
+ PrinterException p = new PrinterException();
+ p.initCause(pe);
+ throw p;
+ }
+ // no printjob active.
+ printJob = null;
+ }
+
+ /**
+ * Prints the page with given attributes.
+ */
+ public void print (PrintRequestAttributeSet attributes)
+ throws PrinterException
+ {
+ this.attributes = attributes;
+ print();
+ }
+
+ /**
+ * Displays a dialog box to the user which allows the print job
+ * attributes to be modified.
+ *
+ * @return <code>false</code> if the user cancels the dialog box,
+ * <code>true</code> otherwise.
+ */
+ public boolean printDialog() throws HeadlessException
+ {
+ return printDialog( attributes );
+ }
+
+ /**
+ * Displays a dialog box to the user which allows the print job
+ * attributes to be modified.
+ *
+ * @return <code>false</code> if the user cancels the dialog box,
+ * <code>true</code> otherwise.
+ */
+ public boolean printDialog(PrintRequestAttributeSet attributes)
+ throws HeadlessException
+ {
+ PrintService chosenPrinter = ServiceUI.printDialog
+ (null, 50, 50, services, null,
+ DocFlavor.INPUT_STREAM.POSTSCRIPT, attributes);
+
+ getPageAttributes();
+
+ if( chosenPrinter != null )
+ {
+ try
+ {
+ setPrintService( chosenPrinter );
+ }
+ catch(PrinterException pe)
+ {
+ // Should not happen.
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * This sets the pages that are to be printed.
+ *
+ * @param pageable The pages to be printed, which may not be <code>null</code>.
+ */
+ public void setPageable(Pageable pageable)
+ {
+ if( pageable == null )
+ throw new NullPointerException("Pageable cannot be null.");
+ this.pageable = pageable;
+ }
+
+ /**
+ * Sets this specified <code>Printable</code> as the one to use for
+ * rendering the pages on the print device.
+ *
+ * @param printable The <code>Printable</code> for the print job.
+ */
+ public void setPrintable(Printable printable)
+ {
+ this.printable = printable;
+ }
+
+ /**
+ * Sets the <code>Printable</code> and the page format for the pages
+ * to be printed.
+ *
+ * @param printable The <code>Printable</code> for the print job.
+ * @param page_format The <code>PageFormat</code> for the print job.
+ */
+ public void setPrintable(Printable printable, PageFormat page_format)
+ {
+ this.printable = printable;
+ this.pageFormat = page_format;
+ }
+
+ /**
+ * Makes any alterations to the specified <code>PageFormat</code>
+ * necessary to make it work with the current printer. The alterations
+ * are made to a clone of the input object, which is then returned.
+ *
+ * @param page_format The <code>PageFormat</code> to validate.
+ *
+ * @return The validated <code>PageFormat</code>.
+ */
+ public PageFormat validatePage(PageFormat page_format)
+ {
+ // FIXME
+ return page_format;
+ }
+
+ /**
+ * Change the printer for this print job to service. Subclasses that
+ * support setting the print service override this method. Throws
+ * PrinterException when the class doesn't support setting the printer,
+ * the service doesn't support Pageable or Printable interfaces for 2D
+ * print output.
+ * @param service The new printer to use.
+ * @throws PrinterException if service is not valid.
+ */
+ public void setPrintService(PrintService service)
+ throws PrinterException
+ {
+ if(!service.isDocFlavorSupported(DocFlavor.INPUT_STREAM.POSTSCRIPT))
+ throw new PrinterException("This printer service is not supported.");
+ printer = service;
+ }
+}
diff --git a/gnu/java/awt/print/PostScriptGraphics2D.java b/gnu/java/awt/print/PostScriptGraphics2D.java
new file mode 100644
index 000000000..2303f44b7
--- /dev/null
+++ b/gnu/java/awt/print/PostScriptGraphics2D.java
@@ -0,0 +1,1349 @@
+/* PostScriptGraphics2D.java -- AWT printer rendering class.
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.java.awt.print;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Composite;
+import java.awt.Paint;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.GradientPaint;
+import java.awt.Graphics;
+import java.awt.GraphicsConfiguration;
+import java.awt.Graphics2D;
+import java.awt.Image;
+import java.awt.Polygon;
+import java.awt.Rectangle;
+import java.awt.RenderingHints;
+import java.awt.Shape;
+import java.awt.Stroke;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Arc2D;
+import java.awt.geom.Ellipse2D;
+import java.awt.geom.RoundRectangle2D;
+import java.awt.geom.PathIterator;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+import java.awt.font.FontRenderContext;
+import java.awt.font.GlyphVector;
+import java.awt.font.TextLayout;
+import java.awt.image.BufferedImage;
+import java.awt.image.BufferedImageOp;
+import java.awt.image.renderable.RenderableImage;
+import java.awt.image.RenderedImage;
+import java.awt.image.ImageObserver;
+import java.awt.image.PixelGrabber;
+import java.awt.print.PageFormat;
+import java.awt.print.Pageable;
+import java.awt.print.Paper;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterGraphics;
+import java.awt.print.PrinterJob;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.text.AttributedCharacterIterator;
+import java.util.Map;
+
+/**
+ * Class PostScriptGraphics2D - Class that implements the Graphics2D object,
+ * writing the output to a PostScript or EPS file
+ *
+ * @author Sven de Marothy
+ *
+ */
+class PostScriptGraphics2D extends Graphics2D
+{
+ /**
+ * The associated printer job.
+ */
+ private PrinterJob printerJob;
+
+ /**
+ * Output file.
+ */
+ private PrintWriter out;
+
+ // Graphics data
+ private AffineTransform currentTransform = new AffineTransform();
+ private AffineTransform pageTransform;
+ private RenderingHints renderingHints;
+ private Paint currentPaint = null;
+ private Shape clipShape = null;
+ private Font currentFont = null;
+ private Color currentColor = Color.black;
+ private Color backgroundColor = Color.white;
+ private Stroke currentStroke = null;
+ private static Stroke ordinaryStroke = new BasicStroke(0.0f,
+ BasicStroke.CAP_BUTT,
+ BasicStroke.JOIN_MITER);
+ private float cx; // current drawing position
+ private float cy; // current drawing position
+ private boolean currentFontIsPS; // set if currentFont is one of the above
+
+ // settings
+ private double pageX = 595;
+ private double pageY = 842;
+ private double Y = pageY;
+ private boolean gradientOn = false;
+
+ /**
+ * Constructor
+ *
+ */
+ public PostScriptGraphics2D( PrinterJob pg )
+ {
+ printerJob = pg;
+ // create transform objects
+ pageTransform = new AffineTransform();
+ currentTransform = new AffineTransform();
+
+ /*
+ Create Rendering hints
+ No text aliasing
+ Quality color and rendering
+ Bicubic interpolation
+ Fractional metrics supported
+ */
+ renderingHints = new RenderingHints(null);
+ renderingHints.put(RenderingHints.KEY_RENDERING,
+ RenderingHints.VALUE_RENDER_QUALITY);
+ renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
+ RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
+ renderingHints.put(RenderingHints.KEY_INTERPOLATION,
+ RenderingHints.VALUE_INTERPOLATION_BICUBIC);
+ renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS,
+ RenderingHints.VALUE_FRACTIONALMETRICS_ON);
+ renderingHints.put(RenderingHints.KEY_COLOR_RENDERING,
+ RenderingHints.VALUE_COLOR_RENDER_QUALITY);
+ }
+
+ /**
+ * Spool a document to PostScript.
+ * If Pageable is non-null, it will print that, otherwise it will use
+ * the supplied printable and pageFormat.
+ */
+ public SpooledDocument spoolPostScript(Printable printable,
+ PageFormat pageFormat,
+ Pageable pageable)
+ throws PrinterException
+ {
+ try
+ {
+ // spool to a temporary file
+ File temp = File.createTempFile("cpspool", ".ps");
+ temp.deleteOnExit();
+
+ out = new PrintWriter(new BufferedWriter
+ (new OutputStreamWriter
+ (new FileOutputStream(temp),
+ "ISO8859_1"), 1000000));
+
+ writePSHeader();
+
+ if(pageable != null)
+ {
+ for(int index = 0; index < pageable.getNumberOfPages(); index++)
+ spoolPage(out, pageable.getPrintable(index),
+ pageable.getPageFormat(index), index);
+ }
+ else
+ {
+ int index = 0;
+ while(spoolPage(out, printable, pageFormat, index++) ==
+ Printable.PAGE_EXISTS);
+ }
+ out.println("%%Trailer");
+ out.println("%%EOF");
+ out.close();
+ return new SpooledDocument( temp );
+ }
+ catch (IOException e)
+ {
+ PrinterException pe = new PrinterException();
+ pe.initCause(e);
+ throw pe;
+ }
+ }
+
+ //--------------------------------------------------------------------------
+
+ /**
+ * Write the postscript file header,
+ * setup the page format and transforms.
+ */
+ private void writePSHeader()
+ {
+ out.println("%!PS-Adobe-3.0");
+ out.println("%%Title: "+printerJob.getJobName());
+ out.println("%%Creator: GNU Classpath ");
+ out.println("%%DocumentData: Clean8Bit");
+
+ out.println("%%DocumentNeededResources: font Times-Roman Helvetica Courier");
+ out.println("%%EndComments");
+
+ out.println("%%BeginProlog");
+ out.println("%%EndProlog");
+ out.println("%%BeginSetup");
+
+ out.println("%%EndFeature");
+ setupFonts();
+ out.println("%%EndSetup");
+
+ // set default fonts and colors
+ setFont( new Font("Dialog", Font.PLAIN, 12) );
+ currentColor = Color.white;
+ currentStroke = new BasicStroke();
+ setPaint(currentColor);
+ setStroke(currentStroke);
+ }
+
+ /**
+ * setupFonts - set up the font dictionaries for
+ * helvetica, times and courier
+ */
+ private void setupFonts()
+ {
+ out.println("/helveticaISO");
+ out.println("/Helvetica findfont dup length dict begin");
+ out.println("{ 1 index /FID eq { pop pop } { def } ifelse } forall");
+ out.println("/Encoding ISOLatin1Encoding def");
+ out.println("currentdict end definefont pop");
+
+ out.println("/timesISO");
+ out.println("/Times-Roman findfont dup length dict begin");
+ out.println("{ 1 index /FID eq { pop pop } { def } ifelse } forall");
+ out.println("/Encoding ISOLatin1Encoding def");
+ out.println("currentdict end definefont pop");
+
+ out.println("/courierISO");
+ out.println("/Courier findfont dup length dict begin");
+ out.println("{ 1 index /FID eq { pop pop } { def } ifelse } forall");
+ out.println("/Encoding ISOLatin1Encoding def");
+ out.println("currentdict end definefont pop");
+ }
+
+ /**
+ * Spools a single page, returns NO_SUCH_PAGE unsuccessful,
+ * PAGE_EXISTS if it was.
+ */
+ public int spoolPage(PrintWriter out,
+ Printable printable,
+ PageFormat pageFormat,
+ int index) throws IOException, PrinterException
+ {
+ out.println("%%BeginPageSetup");
+
+ Paper p = pageFormat.getPaper();
+ pageX = p.getWidth();
+ pageY = p.getHeight();
+
+ if( pageFormat.getOrientation() == PageFormat.PORTRAIT )
+ out.println( "%%Orientation: Portrait" );
+ else
+ {
+ out.println( "%%Orientation: Landscape" );
+ double t = pageX;
+ pageX = pageY;
+ pageY = t;
+ }
+
+ setClip(0, 0, (int)pageX, (int)pageY);
+
+ out.println("gsave % first save");
+
+ // 595x842; 612x792 respectively
+ out.println("<< /PageSize [" +pageX + " "+pageY+ "] >> setpagedevice");
+
+ if( pageFormat.getOrientation() != PageFormat.LANDSCAPE )
+ {
+ pageTransform.translate(pageX, 0);
+ pageTransform.scale(-1.0, 1.0);
+ }
+
+ // save the original CTM
+ pushCTM();
+ concatCTM(pageTransform);
+ setTransform(new AffineTransform());
+
+ out.println("%%EndPageSetup");
+
+ out.println("gsave");
+
+ if( printable.print(this, pageFormat, index) == Printable.NO_SUCH_PAGE )
+ return Printable.NO_SUCH_PAGE;
+
+ out.println("grestore");
+ out.println("showpage");
+
+ return Printable.PAGE_EXISTS;
+ }
+
+ /** push the Current Transformation Matrix onto the PS stack */
+ private void pushCTM()
+ {
+ out.println("matrix currentmatrix % pushCTM()");
+ }
+
+ /** pop the Current Transformation Matrix from the PS stack */
+ private void popCTM()
+ {
+ out.println("setmatrix % restore CTM");
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+
+ public Graphics create()
+ {
+ return null;
+ }
+
+ public void drawOval(int x, int y, int width, int height)
+ {
+ out.println("% drawOval()");
+ setStroke(ordinaryStroke);
+ draw(new Ellipse2D.Double(x, y, width, height));
+ setStroke(currentStroke);
+ }
+
+ public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
+ {
+ if (nPoints <= 0 || xPoints.length < nPoints || yPoints.length < nPoints)
+ return;
+ out.println("newpath % drawPolyLine()");
+ out.println(xPoints[0] + " " + yPoints[0] + " moveto");
+ for (int i = 1; i < nPoints; i++)
+ out.println(xPoints[i] + " " + yPoints[i] + " lineto");
+ out.println("closepath");
+ out.println("stroke");
+ }
+
+ public void drawRoundRect(int x, int y, int width, int height, int arcWidth,
+ int arcHeight)
+ {
+ out.println("% drawRoundRect()");
+ RoundRectangle2D.Double rr = new RoundRectangle2D.Double(x, y, width,
+ height, arcWidth,
+ arcHeight);
+ setStroke(ordinaryStroke);
+ draw(rr);
+ setStroke(currentStroke);
+ }
+
+ public void fillRoundRect(int x, int y, int width, int height, int arcWidth,
+ int arcHeight)
+ {
+ out.println("% fillRoundRect()");
+ RoundRectangle2D.Double rr = new RoundRectangle2D.Double(x, y, width,
+ height, arcWidth,
+ arcHeight);
+ fill(rr);
+ }
+
+ public void drawArc(int x, int y, int width, int height, int startAngle,
+ int arcAngle)
+ {
+ setStroke(ordinaryStroke);
+ draw(new Arc2D.Double(x, y, width, height, startAngle, arcAngle, Arc2D.OPEN));
+ setStroke(currentStroke);
+ }
+
+ public void fillArc(int x, int y, int width, int height, int startAngle,
+ int arcAngle)
+ {
+ fill(new Arc2D.Double(x, y, width, height, startAngle, arcAngle, Arc2D.PIE));
+ }
+
+ public void fillOval(int x, int y, int width, int height)
+ {
+ out.println("% fillOval()");
+ fill( new Ellipse2D.Double(x, y, width, height) );
+ }
+
+ public void fillPolygon(int[] x, int[] y, int nPoints)
+ {
+ out.println("% fillPolygon()");
+ fill( new Polygon(x, y, nPoints) );
+ }
+
+ public void drawLine(int x1, int y1, int x2, int y2)
+ {
+ out.println("% drawLine()");
+ setStroke(ordinaryStroke);
+ out.println("newpath");
+ out.println(x1 + " " + (y1) + " moveto");
+ out.println(x2 + " " + (y2) + " lineto");
+ out.println("stroke");
+ setStroke(currentStroke);
+ }
+
+ //--------------- Image drawing ------------------------------------------
+ public boolean drawImage(Image img, int x, int y, Color bgcolor,
+ ImageObserver observer)
+ {
+ int w = img.getWidth(null);
+ int h = img.getHeight(null);
+
+ return drawImage(img, x, y, x + w, y + h, 0, 0, w - 1, h - 1, bgcolor,
+ observer);
+ }
+
+ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
+ int sx1, int sy1, int sx2, int sy2, Color bgcolor,
+ ImageObserver observer)
+ {
+ int n = 0;
+ boolean flipx = false;
+ boolean flipy = false;
+
+ // swap X and Y's
+ if (sx1 > sx2)
+ {
+ n = sx1;
+ sx1 = sx2;
+ sx2 = n;
+ flipx = ! flipx;
+ }
+ if (sy1 > sy2)
+ {
+ n = sy1;
+ sy1 = sy2;
+ sy2 = n;
+ flipy = ! flipy;
+ }
+ if (dx1 > dx2)
+ {
+ n = dx1;
+ dx1 = dx2;
+ dx2 = n;
+ flipx = ! flipx;
+ }
+ if (dy1 > dy2)
+ {
+ n = dy1;
+ dy1 = dy2;
+ dy2 = n;
+ flipy = ! flipy;
+ }
+ n = 0;
+ int sw = sx2 - sx1; // source width
+ int sh = sy2 - sy1; // source height
+ int[] pixels = new int[sw * sh]; // pixel buffer
+ int dw = dx2 - dx1; // destination width
+ int dh = dy2 - dy1; // destination height
+ double x_scale = ((double) dw) / ((double) sw);
+ double y_scale = ((double) dh) / ((double) sh);
+
+ out.println("% drawImage() 2");
+ out.println("gsave");
+ out.println(dx1 + " " + dy1 + " translate");
+ out.println(dw + " " + dh + " scale");
+ out.println(sw + " " + sh + " 8 [" + (flipx ? -sw : sw) + " 0 0 "
+ + (flipy ? -sh : sh) + " " + (flipx ? sw : 0) + " "
+ + (flipy ? sh : 0) + " ]");
+ out.println("{currentfile 3 string readhexstring pop} bind");
+ out.println("false 3 colorimage");
+
+ PixelGrabber pg = new PixelGrabber(img, sx1, sy1, sw, sh, pixels, 0, sw);
+ try
+ {
+ pg.grabPixels();
+ }
+ catch (InterruptedException e)
+ {
+ System.err.println("interrupted waiting for pixels!");
+ return (false);
+ }
+
+ if ((pg.getStatus() & ImageObserver.ABORT) != 0)
+ {
+ System.err.println("image fetch aborted or errored");
+ return (false);
+ }
+
+ for (int j = 0; j < sh; j++)
+ {
+ for (int i = 0; i < sw; i++)
+ {
+ out.print(colorTripleHex(new Color(pixels[j * sw + i])));
+ if (((++n) % 11) == 0)
+ out.println();
+ }
+ }
+
+ out.println();
+ out.println("%%EOF");
+ out.println("grestore");
+ return true;
+ }
+
+ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2,
+ int sx1, int sy1, int sx2, int sy2,
+ ImageObserver observer)
+ {
+ return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null,
+ observer);
+ }
+
+ public boolean drawImage(Image img, int x, int y, ImageObserver observer)
+ {
+ return drawImage(img, x, y, null, observer);
+ }
+
+ public boolean drawImage(Image img, int x, int y, int width, int height,
+ Color bgcolor, ImageObserver observer)
+ {
+ int sw = img.getWidth(null);
+ int sh = img.getHeight(null);
+ return drawImage(img, x, y, x + width, y + height, /* destination */
+ 0, 0, sw - 1, sh - 1, /* source */
+ bgcolor, observer);
+ // correct?
+ }
+
+ public boolean drawImage(Image img, int x, int y, int width, int height,
+ ImageObserver observer)
+ {
+ return drawImage(img, x, y, width, height, null, observer);
+ }
+
+ /** Renders a BufferedImage that is filtered with a BufferedImageOp. */
+ public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
+ {
+ BufferedImage result = op.filter(img, null);
+ drawImage(result, x, y, null);
+ }
+
+ /** Renders an image, applying a transform from image space
+ into user space before drawing. */
+ public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
+ {
+ AffineTransform oldTransform = new AffineTransform(currentTransform);
+ boolean ret;
+
+ transform(xform);
+ ret = drawImage(img, 0, 0, null, obs);
+ setTransform(oldTransform);
+
+ return ret;
+ }
+
+ /** Renders a RenderableImage, applying a transform from image
+ space into user space before drawing. */
+ public void drawRenderableImage(RenderableImage img, AffineTransform xform)
+ {
+ // FIXME
+ }
+
+ /** Renders a RenderedImage, applying a transform from
+ image space into user space before drawing. */
+ public void drawRenderedImage(RenderedImage img, AffineTransform xform)
+ {
+ // FIXME
+ }
+
+ //-------------------------------------------------------------------------
+ public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
+ {
+ setStroke(ordinaryStroke);
+ draw(new Polygon(xPoints, yPoints, nPoints));
+ setStroke(currentStroke);
+ }
+
+ public void drawString(String str, int x, int y)
+ {
+ drawString(str, (float) x, (float) y);
+ }
+
+ public void drawString(String str, float x, float y)
+ {
+ if( str.trim().equals("") )
+ return; // don't draw whitespace, silly!
+
+ if( currentFontIsPS )
+ {
+ drawStringPSFont(str, x, y);
+ return;
+ }
+
+ TextLayout text = new TextLayout(str, currentFont, getFontRenderContext());
+ Shape s = text.getOutline(AffineTransform.getTranslateInstance(x, y));
+ drawStringShape(s);
+ }
+
+ private void drawStringPSFont(String str, float x, float y)
+ {
+ out.println("% drawString PS font");
+ out.println(x + " " + y + " moveto");
+ saveAndInvertAxis();
+ out.println("(" + str + ") show");
+ restoreAxis();
+ }
+
+ private void saveAndInvertAxis()
+ {
+ // Invert the Y axis of the CTM.
+ popCTM();
+ pushCTM();
+
+ double[] test =
+ {
+ pageTransform.getScaleX(), pageTransform.getShearY(),
+ pageTransform.getShearX(), pageTransform.getScaleY(),
+ pageTransform.getTranslateX(),
+ -pageTransform.getTranslateY() + pageY
+ };
+
+ double[] test2 =
+ {
+ currentTransform.getScaleX(),
+ currentTransform.getShearY(),
+ -currentTransform.getShearX(),
+ -currentTransform.getScaleY(),
+ currentTransform.getTranslateX(),
+ currentTransform.getTranslateY()
+ };
+
+ AffineTransform total = new AffineTransform(test);
+ total.concatenate(new AffineTransform(test2));
+ concatCTM(total);
+ }
+
+ private void restoreAxis()
+ {
+ // reset the CTM
+ popCTM();
+ pushCTM();
+ AffineTransform total = new AffineTransform(pageTransform);
+ total.concatenate(currentTransform);
+ concatCTM(total);
+ }
+
+ /**
+ * special drawing routine for string shapes,
+ * which need to be drawn with the Y axis uninverted.
+ */
+ private void drawStringShape(Shape s)
+ {
+ saveAndInvertAxis();
+
+ // draw the shape s with an inverted Y axis.
+ PathIterator pi = s.getPathIterator(new AffineTransform());
+ float[] coords = new float[6];
+
+ while (! pi.isDone())
+ {
+ switch (pi.currentSegment(coords))
+ {
+ case PathIterator.SEG_MOVETO:
+ out.println((coords[0]) + " " + (Y - coords[1]) + " moveto");
+ cx = coords[0];
+ cy = coords[1];
+ break;
+ case PathIterator.SEG_LINETO:
+ out.println((coords[0]) + " " + (Y - coords[1]) + " lineto");
+ cx = coords[0];
+ cy = coords[1];
+ break;
+ case PathIterator.SEG_QUADTO:
+ // convert to cubic bezier points
+ float x1 = (cx + 2 * coords[0]) / 3;
+ float y1 = (cy + 2 * coords[1]) / 3;
+ float x2 = (2 * coords[2] + coords[0]) / 3;
+ float y2 = (2 * coords[3] + coords[1]) / 3;
+
+ out.print((x1) + " " + (Y - y1) + " ");
+ out.print((x2) + " " + (Y - y2) + " ");
+ out.println((coords[2]) + " " + (Y - coords[3]) + " curveto");
+ cx = coords[2];
+ cy = coords[3];
+ break;
+ case PathIterator.SEG_CUBICTO:
+ out.print((coords[0]) + " " + (Y - coords[1]) + " ");
+ out.print((coords[2]) + " " + (Y - coords[3]) + " ");
+ out.println((coords[4]) + " " + (Y - coords[5]) + " curveto");
+ cx = coords[4];
+ cy = coords[5];
+ break;
+ case PathIterator.SEG_CLOSE:
+ out.println("closepath");
+ break;
+ }
+ pi.next();
+ }
+ out.println("fill");
+
+ restoreAxis();
+ }
+
+ public void setColor(Color c)
+ {
+ /* don't set the color if it's already set */
+ if (c.equals(currentColor))
+ return;
+ gradientOn = false;
+ currentColor = c;
+ currentPaint = c; // Graphics2D extends colors to paint
+
+ out.println(colorTriple(c) + " setrgbcolor");
+ }
+
+ public void clearRect(int x, int y, int width, int height)
+ {
+ out.println("% clearRect");
+ Color c = currentColor;
+ setColor(backgroundColor);
+ fill(new Rectangle2D.Double(x, y, width, height));
+ setColor(c);
+ }
+
+ public void clipRect(int x, int y, int width, int height)
+ {
+ clip(new Rectangle2D.Double(x, y, width, height));
+ }
+
+ public void copyArea(int x, int y, int width, int height, int dx, int dy)
+ {
+ // FIXME
+ }
+
+ public void fillRect(int x, int y, int width, int height)
+ {
+ fill(new Rectangle2D.Double(x, y, width, height));
+ }
+
+ public void dispose()
+ {
+ }
+
+ public void setClip(int x, int y, int width, int height)
+ {
+ out.println("% setClip()");
+ setClip(new Rectangle2D.Double(x, y, width, height));
+ }
+
+ public void setClip(Shape s)
+ {
+ clip(s);
+ }
+
+ public Shape getClip()
+ {
+ return clipShape;
+ }
+
+ public Rectangle getClipBounds()
+ {
+ return clipShape.getBounds();
+ }
+
+ public Color getColor()
+ {
+ return currentColor;
+ }
+
+ public Font getFont()
+ {
+ return currentFont;
+ }
+
+ public FontMetrics getFontMetrics()
+ {
+ return getFontMetrics(currentFont);
+ }
+
+ public FontMetrics getFontMetrics(Font f)
+ {
+ // FIXME
+ return null;
+ }
+
+ public void setFont(Font font)
+ {
+ out.println("% setfont()");
+ if (font == null)
+ // use the default font
+ font = new Font("Dialog", Font.PLAIN, 12);
+ currentFont = font;
+ setPSFont(); // set up the PostScript fonts
+ }
+
+ /**
+ * Setup the postscript font if the current font is one
+ */
+ private void setPSFont()
+ {
+ currentFontIsPS = false;
+
+ String s = currentFont.getName();
+ out.println("% setPSFont: Fontname: " + s);
+ if (s.equalsIgnoreCase("Helvetica") || s.equalsIgnoreCase("SansSerif"))
+ out.print("/helveticaISO findfont ");
+ else if (s.equalsIgnoreCase("Times New Roman"))
+ out.print("/timesISO findfont ");
+ else if (s.equalsIgnoreCase("Courier"))
+ out.print("/courierISO findfont ");
+ else
+ return;
+
+ currentFontIsPS = true;
+
+ out.print(currentFont.getSize() + " scalefont ");
+ out.println("setfont");
+ }
+
+ /** XOR mode is not supported */
+ public void setPaintMode()
+ {
+ }
+
+ /** XOR mode is not supported */
+ public void setXORMode(Color c1)
+ {
+ }
+
+ public void close()
+ {
+ out.println("showpage");
+ out.println("%%Trailer");
+ out.println("grestore % restore original stuff");
+ out.println("%%EOF");
+
+ try
+ {
+ out.close();
+ }
+ catch (Exception e)
+ {
+ }
+ out = null;
+ }
+
+ //----------------------------------------------------------------
+ // Graphics2D stuff ----------------------------------------------
+
+ /** Sets the values of an arbitrary number of
+ preferences for the rendering algorithms. */
+ public void addRenderingHints(Map hints)
+ {
+ /* rendering hint changes are disallowed */
+ }
+
+ /** write a shape to the file */
+ private void writeShape(Shape s)
+ {
+ PathIterator pi = s.getPathIterator(new AffineTransform());
+ float[] coords = new float[6];
+
+ while (! pi.isDone())
+ {
+ switch (pi.currentSegment(coords))
+ {
+ case PathIterator.SEG_MOVETO:
+ out.println(coords[0] + " " + (coords[1]) + " moveto");
+ cx = coords[0];
+ cy = coords[1];
+ break;
+ case PathIterator.SEG_LINETO:
+ out.println(coords[0] + " " + (coords[1]) + " lineto");
+ cx = coords[0];
+ cy = coords[1];
+ break;
+ case PathIterator.SEG_QUADTO:
+ // convert to cubic bezier points
+ float x1 = (cx + 2 * coords[0]) / 3;
+ float y1 = (cy + 2 * coords[1]) / 3;
+ float x2 = (2 * coords[2] + coords[0]) / 3;
+ float y2 = (2 * coords[3] + coords[1]) / 3;
+
+ out.print(x1 + " " + (Y - y1) + " ");
+ out.print(x2 + " " + (Y - y2) + " ");
+ out.println(coords[2] + " " + (Y - coords[3]) + " curveto");
+ cx = coords[2];
+ cy = coords[3];
+ break;
+ case PathIterator.SEG_CUBICTO:
+ out.print(coords[0] + " " + coords[1] + " ");
+ out.print(coords[2] + " " + coords[3] + " ");
+ out.println(coords[4] + " " + coords[5] + " curveto");
+ cx = coords[4];
+ cy = coords[5];
+ break;
+ case PathIterator.SEG_CLOSE:
+ out.println("closepath");
+ break;
+ }
+ pi.next();
+ }
+ }
+
+ /** Intersects the current Clip with the interior of
+ the specified Shape and sets the Clip to the resulting intersection. */
+ public void clip(Shape s)
+ {
+ clipShape = s;
+ out.println("% clip INACTIVE");
+ // writeShape(s);
+ // out.println("clip");
+ }
+
+ /** Strokes the outline of a Shape using the
+ settings of the current Graphics2D context.*/
+ public void draw(Shape s)
+ {
+ if(!(currentStroke instanceof BasicStroke))
+ fill(currentStroke.createStrokedShape(s));
+
+ out.println("% draw");
+ writeShape(s);
+ out.println("stroke");
+ }
+
+ /** Renders the text of the specified GlyphVector using the
+ Graphics2D context's rendering attributes. */
+ public void drawGlyphVector(GlyphVector gv, float x, float y)
+ {
+ out.println("% drawGlyphVector");
+ Shape s = gv.getOutline();
+ drawStringShape(AffineTransform.getTranslateInstance(x, y)
+ .createTransformedShape(s));
+ }
+
+ /** Renders the text of the specified iterator,
+ using the Graphics2D context's current Paint.*/
+ public void drawString(AttributedCharacterIterator iterator, float x, float y)
+ {
+ TextLayout text = new TextLayout(iterator, getFontRenderContext());
+ Shape s = text.getOutline(AffineTransform.getTranslateInstance(x, y));
+ drawStringShape(s);
+ }
+
+ /** Renders the text of the specified iterator,
+ using the Graphics2D context's current Paint. */
+ public void drawString(AttributedCharacterIterator iterator, int x, int y)
+ {
+ drawString(iterator, (float) x, (float) y);
+ }
+
+ /** Fills the interior of a Shape using the settings of the Graphics2D context. */
+ public void fill(Shape s)
+ {
+ out.println("% fill");
+ if (! gradientOn)
+ {
+ writeShape(s);
+ out.println("fill");
+ }
+ else
+ {
+ out.println("gsave");
+ writeShape(s);
+ out.println("clip");
+ writeGradient();
+ out.println("shfill");
+ out.println("grestore");
+ }
+ }
+
+ /** Returns the background color used for clearing a region. */
+ public Color getBackground()
+ {
+ return backgroundColor;
+ }
+
+ /** Returns the current Composite in the Graphics2D context. */
+ public Composite getComposite()
+ {
+ // FIXME
+ return null;
+ }
+
+ /** Returns the device configuration associated with this Graphics2D. */
+ public GraphicsConfiguration getDeviceConfiguration()
+ {
+ // FIXME
+ out.println("% getDeviceConfiguration()");
+ return null;
+ }
+
+ /** Get the rendering context of the Font within this Graphics2D context. */
+ public FontRenderContext getFontRenderContext()
+ {
+ out.println("% getFontRenderContext()");
+
+ double[] scaling =
+ {
+ pageTransform.getScaleX(), 0, 0,
+ -pageTransform.getScaleY(), 0, 0
+ };
+
+ return (new FontRenderContext(new AffineTransform(scaling), false, true));
+ }
+
+ /** Returns the current Paint of the Graphics2D context. */
+ public Paint getPaint()
+ {
+ return currentPaint;
+ }
+
+ /** Returns the value of a single preference for the rendering algorithms. */
+ public Object getRenderingHint(RenderingHints.Key hintKey)
+ {
+ return renderingHints.get(hintKey);
+ }
+
+ /** Gets the preferences for the rendering algorithms. */
+ public RenderingHints getRenderingHints()
+ {
+ return renderingHints;
+ }
+
+ /** Returns the current Stroke in the Graphics2D context. */
+ public Stroke getStroke()
+ {
+ return currentStroke;
+ }
+
+ /** Returns a copy of the current Transform in the Graphics2D context. */
+ public AffineTransform getTransform()
+ {
+ return currentTransform;
+ }
+
+ /**
+ * Checks whether or not the specified Shape intersects
+ * the specified Rectangle, which is in device space.
+ */
+ public boolean hit(Rectangle rect, Shape s, boolean onStroke)
+ {
+ Rectangle2D.Double r = new Rectangle2D.Double(rect.getX(), rect.getY(),
+ rect.getWidth(),
+ rect.getHeight());
+ return s.intersects(r);
+ }
+
+ /** Sets the background color for the Graphics2D context.*/
+ public void setBackground(Color color)
+ {
+ out.println("% setBackground(" + color + ")");
+ backgroundColor = color;
+ }
+
+ /** Sets the Composite for the Graphics2D context.
+ Not supported. */
+ public void setComposite(Composite comp)
+ {
+ }
+
+ /** Sets the Paint attribute for the Graphics2D context.*/
+ public void setPaint(Paint paint)
+ {
+ currentPaint = paint;
+ gradientOn = false;
+ if (paint instanceof Color)
+ {
+ setColor((Color) paint);
+ return;
+ }
+ if (paint instanceof GradientPaint)
+ {
+ gradientOn = true;
+ return;
+ }
+ }
+
+ /* get a space seperated 0.0 - 1.0 color RGB triple */
+ private String colorTriple(Color c)
+ {
+ return (((double) c.getRed() / 255.0) + " "
+ + ((double) c.getGreen() / 255.0) + " "
+ + ((double) c.getBlue() / 255.0));
+ }
+
+ /**
+ * Get a nonsperated hex RGB triple, eg FFFFFF = white
+ * used by writeGradient and drawImage
+ */
+ private String colorTripleHex(Color c)
+ {
+ String r = "00" + Integer.toHexString(c.getRed());
+ r = r.substring(r.length() - 2);
+ String g = "00" + Integer.toHexString(c.getGreen());
+ g = g.substring(g.length() - 2);
+ String b = "00" + Integer.toHexString(c.getBlue());
+ b = b.substring(b.length() - 2);
+ return r + g + b;
+ }
+
+ /* write the current gradient fill */
+ private void writeGradient()
+ {
+ GradientPaint paint = (GradientPaint) currentPaint;
+ out.println("% writeGradient()");
+
+ int n = 1;
+ double x;
+ double y;
+ double dx;
+ double dy;
+ Point2D p1 = currentTransform.transform(paint.getPoint1(), null);
+ Point2D p2 = currentTransform.transform(paint.getPoint2(), null);
+ x = p1.getX();
+ y = p1.getY();
+ dx = p2.getX() - x;
+ dy = p2.getY() - y;
+
+ // get number of repetitions
+ while (x + n * dx < pageY && y + n * dy < pageX && x + n * dx > 0
+ && y + n * dy > 0)
+ n++;
+
+ out.println("<<"); // start
+ out.println("/ShadingType 2"); // gradient fill
+ out.println("/ColorSpace [ /DeviceRGB ]"); // RGB colors
+ out.print("/Coords [");
+ out.print(x + " " + y + " " + (x + n * dx) + " " + (y + n * dy) + " ");
+ out.println("]"); // coordinates defining the axis
+ out.println("/Function <<");
+ out.println("/FunctionType 0");
+ out.println("/Order 1");
+ out.println("/Domain [ 0 1 ]");
+ out.println("/Range [ 0 1 0 1 0 1 ]");
+ out.println("/BitsPerSample 8");
+ out.println("/Size [ " + (1 + n) + " ]");
+ out.print("/DataSource < " + colorTripleHex(paint.getColor1()) + " "
+ + colorTripleHex(paint.getColor2()) + " ");
+ for (; n > 1; n--)
+ if (paint.isCyclic())
+ {
+ if ((n % 2) == 1)
+ out.print(colorTripleHex(paint.getColor1()) + " ");
+ else
+ out.print(colorTripleHex(paint.getColor2()) + " ");
+ }
+ else
+ out.print(colorTripleHex(paint.getColor2()) + " ");
+ out.println(">");
+ out.println(">>");
+ out.println(">>");
+ }
+
+ /** Sets the value of a single preference for the rendering algorithms. */
+ public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
+ {
+ /* we don't allow the changing of rendering hints. */
+ }
+
+ /** Replaces the values of all preferences for the rendering algorithms
+ with the specified hints. */
+ public void setRenderingHints(Map hints)
+ {
+ /* we don't allow the changing of rendering hints. */
+ }
+
+ /**
+ * Sets the Stroke for the Graphics2D context. BasicStroke fully implemented.
+ */
+ public void setStroke(Stroke s)
+ {
+ currentStroke = s;
+
+ if (! (s instanceof BasicStroke))
+ return;
+
+ BasicStroke bs = (BasicStroke) s;
+ out.println("% setStroke()");
+ try
+ {
+ // set the line width
+ out.println(bs.getLineWidth() + " setlinewidth");
+
+ // set the line dash
+ float[] dashArray = bs.getDashArray();
+ if (dashArray != null)
+ {
+ out.print("[ ");
+ for (int i = 0; i < dashArray.length; i++)
+ out.print(dashArray[i] + " ");
+ out.println("] " + bs.getDashPhase() + " setdash");
+ }
+ else
+ out.println("[] 0 setdash"); // set solid
+
+ // set the line cap
+ switch (bs.getEndCap())
+ {
+ case BasicStroke.CAP_BUTT:
+ out.println("0 setlinecap");
+ break;
+ case BasicStroke.CAP_ROUND:
+ out.println("1 setlinecap");
+ break;
+ case BasicStroke.CAP_SQUARE:
+ out.println("2 setlinecap");
+ break;
+ }
+
+ // set the line join
+ switch (bs.getLineJoin())
+ {
+ case BasicStroke.JOIN_BEVEL:
+ out.println("2 setlinejoin");
+ break;
+ case BasicStroke.JOIN_MITER:
+ out.println("0 setlinejoin");
+ out.println(bs.getMiterLimit() + " setmiterlimit");
+ break;
+ case BasicStroke.JOIN_ROUND:
+ out.println("1 setlinejoin");
+ break;
+ }
+ }
+ catch (Exception e)
+ {
+ out.println("% Exception in setStroke()");
+ }
+ }
+
+ //////////////////// TRANSFORM SETTING /////////////////////////////////////
+ private void concatCTM(AffineTransform Tx)
+ {
+ double[] matrixElements = new double[6];
+ Tx.getMatrix(matrixElements);
+
+ out.print("[ ");
+ for (int i = 0; i < 6; i++)
+ out.print(matrixElements[i] + " ");
+ out.println("] concat");
+ }
+
+ /** Sets the Transform in the Graphics2D context. */
+ public void setTransform(AffineTransform Tx)
+ {
+ // set the transformation matrix;
+ currentTransform = Tx;
+
+ // concatenate the current transform and the page transform
+ AffineTransform totalTransform = new AffineTransform(pageTransform);
+ totalTransform.concatenate(currentTransform);
+ out.println("% setTransform()");
+ out.println("% pageTransform:" + pageTransform);
+ out.println("% currentTransform:" + currentTransform);
+ out.println("% totalTransform:" + totalTransform);
+
+ popCTM();
+ pushCTM(); // set the CTM to it's original state
+ concatCTM(totalTransform); // apply our transforms
+ }
+
+ /** Composes an AffineTransform object with the Transform
+ in this Graphics2D according to the rule last-specified-first-applied. */
+ public void transform(AffineTransform Tx)
+ {
+ // concatenate the current transform
+ currentTransform.concatenate(Tx);
+ // and the PS CTM
+ concatCTM(Tx);
+ }
+
+ ////////////////////////// TRANSFORMS //////////////////////////////////////
+
+ /** shear transform */
+ public void shear(double shx, double shy)
+ {
+ out.println("% shear()");
+ AffineTransform Tx = new AffineTransform();
+ Tx.shear(shx, shy);
+ transform(Tx);
+ }
+
+ /** Translates the origin of the Graphics2D context
+ to the point (x, y) in the current coordinate system. */
+ public void translate(int x, int y)
+ {
+ out.println("% translate()");
+ AffineTransform Tx = new AffineTransform();
+ Tx.translate(x, y);
+ transform(Tx);
+ }
+
+ /** Translates the origin of the Graphics2D context
+ to the point (x, y) in the current coordinate system. */
+ public void translate(double x, double y)
+ {
+ out.println("% translate(" + x + ", " + y + ")");
+ AffineTransform Tx = new AffineTransform();
+ Tx.translate(x, y);
+ transform(Tx);
+ }
+
+ /** Concatenates the current Graphics2D Transform with a rotation transform.*/
+ public void rotate(double theta)
+ {
+ out.println("% rotate(" + theta + ")");
+ AffineTransform Tx = new AffineTransform();
+ Tx.rotate(theta);
+ transform(Tx);
+ }
+
+ /** Concatenates the current Graphics2D Transform with
+ a translated rotation transform.*/
+ public void rotate(double theta, double x, double y)
+ {
+ out.println("% rotate()");
+ AffineTransform Tx = new AffineTransform();
+ Tx.rotate(theta, x, y);
+ transform(Tx);
+ }
+
+ /** Concatenates the current Graphics2D Transform with a scaling
+ transformation Subsequent rendering is resized according to the
+ specified scaling factors relative to the previous scaling.*/
+ public void scale(double sx, double sy)
+ {
+ out.println("% scale(" + sx + ", " + sy + ")");
+ AffineTransform Tx = new AffineTransform();
+ Tx.scale(sx, sy);
+ transform(Tx);
+ }
+}
diff --git a/gnu/java/awt/print/SpooledDocument.java b/gnu/java/awt/print/SpooledDocument.java
new file mode 100644
index 000000000..b606a2ef6
--- /dev/null
+++ b/gnu/java/awt/print/SpooledDocument.java
@@ -0,0 +1,91 @@
+/* SpooledDocument.java -- Reurgitate a spooled PostScript file
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.java.awt.print;
+
+import javax.print.Doc;
+import javax.print.DocFlavor;
+import javax.print.attribute.DocAttributeSet;
+import java.io.File;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.Reader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+public class SpooledDocument implements Doc
+{
+ private FileInputStream fis;
+
+ public SpooledDocument(File file)
+ {
+ try
+ {
+ fis = new FileInputStream(file);
+ }
+ catch (FileNotFoundException ffne)
+ {
+ // Shouldn't happen.
+ }
+ }
+
+ public DocAttributeSet getAttributes()
+ {
+ return null;
+ }
+
+ public DocFlavor getDocFlavor()
+ {
+ return DocFlavor.INPUT_STREAM.POSTSCRIPT;
+ }
+
+ public Object getPrintData()
+ {
+ return fis;
+ }
+
+ public Reader getReaderForText()
+ {
+ return new InputStreamReader(fis);
+ }
+
+ public InputStream getStreamForBytes()
+ {
+ return fis;
+ }
+}