Class PSGraphics2D
- java.lang.Object
-
- java.awt.Graphics
-
- java.awt.Graphics2D
-
- org.apache.xmlgraphics.java2d.AbstractGraphics2D
-
- org.apache.xmlgraphics.java2d.ps.PSGraphics2D
-
- All Implemented Interfaces:
java.lang.Cloneable
- Direct Known Subclasses:
AbstractPSDocumentGraphics2D
public class PSGraphics2D extends AbstractGraphics2D
This is a concrete implementation of AbstractGraphics2D (and therefore of Graphics2D) which is able to generate PostScript code.- Version:
- $Id: PSGraphics2D.java 1809627 2017-09-25 13:42:08Z ssteiner $
- See Also:
Originally authored by Keiron Liddle.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanclippingDisabledDisable or enable clippingprotected java.awt.ColorcurrentColourthe current colour for use in svgprotected TextHandlercustomTextHandlerCustom text handlerprotected TextHandlerfallbackTextHandlerFallback text handlerprotected PSGeneratorgenthe PostScript generator being createdprotected java.util.List<java.lang.Integer>pathHashCacheprotected PSGraphics2DrootG2DThe G2D instance that represents the root instance (used in context with create()/dispose()).protected booleanstartCache-
Fields inherited from class org.apache.xmlgraphics.java2d.AbstractGraphics2D
gc, inPossibleRecursion, textAsShapes
-
-
Constructor Summary
Constructors Constructor Description PSGraphics2D(boolean textAsShapes)Create a new Graphics2D that generates PostScript code.PSGraphics2D(boolean textAsShapes, PSGenerator gen)Create a new Graphics2D that generates PostScript code.PSGraphics2D(PSGraphics2D g)Constructor for creating copies
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidapplyPaint(java.awt.Paint paint, boolean fill)Applies a new Paint object.protected voidapplyStroke(java.awt.Stroke stroke)Applies a new Stroke object.static voidapplyStroke(java.awt.Stroke stroke, PSGenerator gen)Applies a new Stroke object.java.awt.image.BufferedImagebuildBufferedImage(java.awt.Dimension size)Creates a buffered image.voidcopyArea(int x, int y, int width, int height, int dx, int dy)Copies an area of the component by a distance specified bydxanddy.java.awt.Graphicscreate()Creates a newGraphicsobject that is a copy of thisGraphicsobject.voiddisableClipping(boolean b)Disable clipping on each draw command.voiddispose()Disposes of this graphics context and releases any system resources that it is using.protected voiddoDrawing(boolean fill, boolean stroke, boolean nonzero)Commits a painting operation.voiddraw(java.awt.Shape s)Strokes the outline of aShapeusing the settings of the currentGraphics2Dcontext.booleandrawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.booleandrawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)Draws as much of the specified image as is currently available.booleandrawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer, java.awt.Color mask)voiddrawRenderableImage(java.awt.image.renderable.RenderableImage img, java.awt.geom.AffineTransform xform)Renders aRenderableImage, applying a transform from image space into user space before drawing.voiddrawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)Renders aRenderedImage, applying a transform from image space into user space before drawing.voiddrawString(java.lang.String s, float x, float y)Renders the text specified by the specifiedString, using the currentFontandPaintattributes in theGraphics2Dcontext.voidestablishColor(java.awt.Color c)Establishes the given color in the PostScript interpreter.voidfill(java.awt.Shape s)Fills the interior of aShapeusing the settings of theGraphics2Dcontext.TextHandlergetCustomTextHandler()java.awt.GraphicsConfigurationgetDeviceConfiguration()Returns the device configuration associated with thisGraphics2D.TextHandlergetFallbackTextHandler()java.awt.FontMetricsgetFontMetrics(java.awt.Font f)Gets the font metrics for the specified font.PSGeneratorgetPSGenerator()voidhandleIOException(java.io.IOException ioe)Central handler for IOExceptions for this class.voidpreparePainting()This method is used by AbstractPSDocumentGraphics2D to prepare a new page if necessary.voidprocessPathIterator(java.awt.geom.PathIterator iter)Processes a path iterator generating the nexessary painting operations.protected voidprocessPathIteratorCached(java.awt.Shape s)protected java.lang.StringprocessPathIteratorToString(java.awt.geom.PathIterator iter)intprocessShape(java.awt.Shape s, boolean cached)Processes a Shape generating the necessary painting operations.voidsetCustomTextHandler(TextHandler handler)Sets a custom TextHandler implementation that is responsible for painting text.voidsetGraphicContext(GraphicContext c)Sets the GraphicContextvoidsetPSGenerator(PSGenerator gen)Sets the PostScript generatorvoidsetXORMode(java.awt.Color c1)Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified color.booleanshouldBeClipped(java.awt.Shape clip, java.awt.Shape s)Determines if a shape interacts with a clipping region.voidwriteClip(java.awt.Shape s)Establishes a clipping region-
Methods inherited from class org.apache.xmlgraphics.java2d.AbstractGraphics2D
addRenderingHints, clearRect, clip, clipRect, drawArc, drawGlyphVector, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawLine, drawOval, drawPolygon, drawPolyline, drawRect, drawRoundRect, drawString, drawString, drawString, fillArc, fillOval, fillPolygon, fillRect, fillRoundRect, getBackground, getClip, getClipBounds, getColor, getComposite, getFont, getFontRenderContext, getGraphicContext, getPaint, getRenderingHint, getRenderingHints, getStroke, getTransform, hit, rotate, rotate, scale, setBackground, setClip, setClip, setColor, setComposite, setFont, setPaint, setPaintMode, setRenderingHint, setRenderingHints, setStroke, setTransform, shear, transform, translate, translate
-
-
-
-
Field Detail
-
pathHashCache
protected java.util.List<java.lang.Integer> pathHashCache
-
startCache
protected boolean startCache
-
rootG2D
protected PSGraphics2D rootG2D
The G2D instance that represents the root instance (used in context with create()/dispose()). Null if this instance is the root instance.
-
gen
protected PSGenerator gen
the PostScript generator being created
-
clippingDisabled
protected boolean clippingDisabled
Disable or enable clipping
-
fallbackTextHandler
protected TextHandler fallbackTextHandler
Fallback text handler
-
customTextHandler
protected TextHandler customTextHandler
Custom text handler
-
currentColour
protected java.awt.Color currentColour
the current colour for use in svg
-
-
Constructor Detail
-
PSGraphics2D
public PSGraphics2D(boolean textAsShapes)
Create a new Graphics2D that generates PostScript code.- Parameters:
textAsShapes- True if text should be rendered as graphics- See Also:
AbstractGraphics2D(boolean)
-
PSGraphics2D
public PSGraphics2D(boolean textAsShapes, PSGenerator gen)Create a new Graphics2D that generates PostScript code.- Parameters:
textAsShapes- True if text should be rendered as graphicsgen- PostScript generator to use for output- See Also:
AbstractGraphics2D(boolean)
-
PSGraphics2D
public PSGraphics2D(PSGraphics2D g)
Constructor for creating copies- Parameters:
g- parent PostScript Graphics2D
-
-
Method Detail
-
setPSGenerator
public void setPSGenerator(PSGenerator gen)
Sets the PostScript generator- Parameters:
gen- the PostScript generator
-
getPSGenerator
public PSGenerator getPSGenerator()
- Returns:
- the PostScript generator used by this instance.
-
setGraphicContext
public void setGraphicContext(GraphicContext c)
Sets the GraphicContext- Parameters:
c- GraphicContext to use
-
getFallbackTextHandler
public TextHandler getFallbackTextHandler()
- Returns:
- the fallback TextHandler implementation
-
getCustomTextHandler
public TextHandler getCustomTextHandler()
- Returns:
- the custom TextHandler implementation
-
setCustomTextHandler
public void setCustomTextHandler(TextHandler handler)
Sets a custom TextHandler implementation that is responsible for painting text. The default TextHandler paints all text as shapes. A custom implementation can implement text painting using text painting operators.- Parameters:
handler- the custom TextHandler implementation
-
disableClipping
public void disableClipping(boolean b)
Disable clipping on each draw command.- Parameters:
b- set to true to disable all clipping.
-
create
public java.awt.Graphics create()
Creates a newGraphicsobject that is a copy of thisGraphicsobject.- Specified by:
createin classjava.awt.Graphics- Returns:
- a new graphics context that is a copy of this graphics context.
-
handleIOException
public void handleIOException(java.io.IOException ioe)
Central handler for IOExceptions for this class.- Parameters:
ioe- IOException to handle
-
preparePainting
public void preparePainting()
This method is used by AbstractPSDocumentGraphics2D to prepare a new page if necessary.
-
drawImage
public boolean drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)Draws as much of the specified image as is currently available. The image is drawn with its top-left corner at (x, y) in this graphics context's coordinate space. Transparent pixels in the image do not affect whatever pixels are already there.This method returns immediately in all cases, even if the complete image has not yet been loaded, and it has not been dithered and converted for the current output device.
If the image has not yet been completely loaded, then
drawImagereturnsfalse. As more of the image becomes available, the process that draws the image notifies the specified image observer.- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the specified image to be drawn.x- the x coordinate.y- the y coordinate.observer- object to be notified as more of the image is converted.- Returns:
- True if the image has been fully drawn/loaded
- See Also:
Image,ImageObserver,ImageObserver.imageUpdate(java.awt.Image, int, int, int, int, int)
-
drawImage
public boolean drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer, java.awt.Color mask)
-
buildBufferedImage
public java.awt.image.BufferedImage buildBufferedImage(java.awt.Dimension size)
Creates a buffered image.- Parameters:
size- dimensions of the image to be created- Returns:
- the buffered image
-
drawImage
public boolean drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.The image is drawn inside the specified rectangle of this graphics context's coordinate space, and is scaled if necessary. Transparent pixels do not affect whatever pixels are already there.
This method returns immediately in all cases, even if the entire image has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete, then
drawImagereturnsfalse. As more of the image becomes available, the process that draws the image notifies the image observer by calling itsimageUpdatemethod.A scaled version of an image will not necessarily be available immediately just because an unscaled version of the image has been constructed for this output device. Each size of the image may be cached separately and generated from the original data in a separate image production sequence.
- Specified by:
drawImagein classjava.awt.Graphics- Parameters:
img- the specified image to be drawn.x- the x coordinate.y- the y coordinate.width- the width of the rectangle.height- the height of the rectangle.observer- object to be notified as more of the image is converted.- Returns:
- True if the image has been fully loaded/drawn
- See Also:
Image,ImageObserver,ImageObserver.imageUpdate(java.awt.Image, int, int, int, int, int)
-
dispose
public void dispose()
Disposes of this graphics context and releases any system resources that it is using. AGraphicsobject cannot be used afterdisposehas been called.When a Java program runs, a large number of
Graphicsobjects can be created within a short time frame. Although the finalization process of the garbage collector also disposes of the same system resources, it is preferable to manually free the associated resources by calling this method rather than to rely on a finalization process which may not run to completion for a long period of time.Graphics objects which are provided as arguments to the
paintandupdatemethods of components are automatically released by the system when those methods return. For efficiency, programmers should calldisposewhen finished using aGraphicsobject only if it was created directly from a component or anotherGraphicsobject.- Specified by:
disposein classjava.awt.Graphics- See Also:
Graphics.finalize(),Component.paint(java.awt.Graphics),Component.update(java.awt.Graphics),Component.getGraphics(),Graphics.create()
-
processShape
public int processShape(java.awt.Shape s, boolean cached) throws java.io.IOExceptionProcesses a Shape generating the necessary painting operations.- Parameters:
s- Shape to process- Returns:
- the winding rule of the path defining the shape
- Throws:
java.io.IOException- In case of an I/O problem.
-
processPathIteratorToString
protected java.lang.String processPathIteratorToString(java.awt.geom.PathIterator iter) throws java.io.IOException- Throws:
java.io.IOException
-
processPathIteratorCached
protected void processPathIteratorCached(java.awt.Shape s) throws java.io.IOException- Throws:
java.io.IOException
-
processPathIterator
public void processPathIterator(java.awt.geom.PathIterator iter) throws java.io.IOExceptionProcesses a path iterator generating the nexessary painting operations.- Parameters:
iter- PathIterator to process- Throws:
java.io.IOException- In case of an I/O problem.
-
draw
public void draw(java.awt.Shape s)
Strokes the outline of aShapeusing the settings of the currentGraphics2Dcontext. The rendering attributes applied include theClip,Transform,Paint,CompositeandStrokeattributes.- Specified by:
drawin classjava.awt.Graphics2D- Parameters:
s- theShapeto be rendered- See Also:
AbstractGraphics2D.setStroke(java.awt.Stroke),AbstractGraphics2D.setPaint(java.awt.Paint),Graphics.setColor(java.awt.Color),AbstractGraphics2D.transform(java.awt.geom.AffineTransform),AbstractGraphics2D.setTransform(java.awt.geom.AffineTransform),AbstractGraphics2D.clip(java.awt.Shape),AbstractGraphics2D.setClip(int, int, int, int),AbstractGraphics2D.setComposite(java.awt.Composite)
-
shouldBeClipped
public boolean shouldBeClipped(java.awt.Shape clip, java.awt.Shape s)Determines if a shape interacts with a clipping region.- Parameters:
clip- Shape defining the clipping regions- Shape to be drawn- Returns:
- true if code for a clipping region needs to be generated.
-
writeClip
public void writeClip(java.awt.Shape s)
Establishes a clipping region- Parameters:
s- Shape defining the clipping region
-
applyPaint
protected void applyPaint(java.awt.Paint paint, boolean fill)Applies a new Paint object.- Parameters:
paint- Paint object to usefill- True if to be applied for filling
-
applyStroke
protected void applyStroke(java.awt.Stroke stroke)
Applies a new Stroke object.- Parameters:
stroke- Stroke object to use
-
applyStroke
public static void applyStroke(java.awt.Stroke stroke, PSGenerator gen) throws java.io.IOExceptionApplies a new Stroke object.- Parameters:
stroke- the Stroke instancegen- the PS generator- Throws:
java.io.IOException- if an I/O error occurs
-
drawRenderedImage
public void drawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)Renders aRenderedImage, applying a transform from image space into user space before drawing. The transformation from user space into device space is done with the currentTransformin theGraphics2D. The specified transformation is applied to the image before the transform attribute in theGraphics2Dcontext is applied. The rendering attributes applied include theClip,Transform, andCompositeattributes. Note that no rendering is done if the specified transform is noninvertible.- Specified by:
drawRenderedImagein classjava.awt.Graphics2D- Parameters:
img- the image to be renderedxform- the transformation from image space into user space- See Also:
AbstractGraphics2D.transform(java.awt.geom.AffineTransform),AbstractGraphics2D.setTransform(java.awt.geom.AffineTransform),AbstractGraphics2D.setComposite(java.awt.Composite),AbstractGraphics2D.clip(java.awt.Shape),AbstractGraphics2D.setClip(int, int, int, int)
-
drawRenderableImage
public void drawRenderableImage(java.awt.image.renderable.RenderableImage img, java.awt.geom.AffineTransform xform)Renders aRenderableImage, applying a transform from image space into user space before drawing. The transformation from user space into device space is done with the currentTransformin theGraphics2D. The specified transformation is applied to the image before the transform attribute in theGraphics2Dcontext is applied. The rendering attributes applied include theClip,Transform, andCompositeattributes. Note that no rendering is done if the specified transform is noninvertible.Rendering hints set on the
Graphics2Dobject might be used in rendering theRenderableImage. If explicit control is required over specific hints recognized by a specificRenderableImage, or if knowledge of which hints are used is required, then aRenderedImageshould be obtained directly from theRenderableImageand rendered usingdrawRenderedImage.- Specified by:
drawRenderableImagein classjava.awt.Graphics2D- Parameters:
img- the image to be renderedxform- the transformation from image space into user space- See Also:
AbstractGraphics2D.transform(java.awt.geom.AffineTransform),AbstractGraphics2D.setTransform(java.awt.geom.AffineTransform),AbstractGraphics2D.setComposite(java.awt.Composite),AbstractGraphics2D.clip(java.awt.Shape),AbstractGraphics2D.setClip(int, int, int, int),drawRenderedImage(java.awt.image.RenderedImage, java.awt.geom.AffineTransform)
-
establishColor
public void establishColor(java.awt.Color c) throws java.io.IOExceptionEstablishes the given color in the PostScript interpreter.- Parameters:
c- the color to set- Throws:
java.io.IOException- In case of an I/O problem
-
drawString
public void drawString(java.lang.String s, float x, float y)Renders the text specified by the specifiedString, using the currentFontandPaintattributes in theGraphics2Dcontext. The baseline of the first character is at position (x, y) in the User Space. The rendering attributes applied include theClip,Transform,Paint,FontandCompositeattributes. For characters in script systems such as Hebrew and Arabic, the glyphs can be rendered from right to left, in which case the coordinate supplied is the location of the leftmost character on the baseline.- Specified by:
drawStringin classjava.awt.Graphics2D- Parameters:
s- theStringto be renderedx- the x-coordinate where theStringshould be renderedy- the y-coordinate where theStringshould be rendered- See Also:
AbstractGraphics2D.setPaint(java.awt.Paint),Graphics.setColor(java.awt.Color),Graphics.setFont(java.awt.Font),AbstractGraphics2D.setTransform(java.awt.geom.AffineTransform),AbstractGraphics2D.setComposite(java.awt.Composite),AbstractGraphics2D.setClip(int, int, int, int)
-
fill
public void fill(java.awt.Shape s)
Fills the interior of aShapeusing the settings of theGraphics2Dcontext. The rendering attributes applied include theClip,Transform,Paint, andComposite.- Specified by:
fillin classjava.awt.Graphics2D- Parameters:
s- theShapeto be filled- See Also:
AbstractGraphics2D.setPaint(java.awt.Paint),Graphics.setColor(java.awt.Color),AbstractGraphics2D.transform(java.awt.geom.AffineTransform),AbstractGraphics2D.setTransform(java.awt.geom.AffineTransform),AbstractGraphics2D.setComposite(java.awt.Composite),AbstractGraphics2D.clip(java.awt.Shape),AbstractGraphics2D.setClip(int, int, int, int)
-
doDrawing
protected void doDrawing(boolean fill, boolean stroke, boolean nonzero) throws java.io.IOExceptionCommits a painting operation.- Parameters:
fill- fillingstroke- strokingnonzero- true if the non-zero winding rule should be used when filling- Throws:
java.io.IOException- In case of an I/O problem
-
getDeviceConfiguration
public java.awt.GraphicsConfiguration getDeviceConfiguration()
Returns the device configuration associated with thisGraphics2D.- Specified by:
getDeviceConfigurationin classjava.awt.Graphics2D- Returns:
- the device configuration
-
getFontMetrics
public java.awt.FontMetrics getFontMetrics(java.awt.Font f)
Gets the font metrics for the specified font.- Specified by:
getFontMetricsin classjava.awt.Graphics- Parameters:
f- the specified font- Returns:
- the font metrics for the specified font.
- See Also:
Graphics.getFont(),FontMetrics,Graphics.getFontMetrics()
-
setXORMode
public void setXORMode(java.awt.Color c1)
Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified color. This specifies that logical pixel operations are performed in the XOR mode, which alternates pixels between the current color and a specified XOR color.When drawing operations are performed, pixels which are the current color are changed to the specified color, and vice versa.
Pixels that are of colors other than those two colors are changed in an unpredictable but reversible manner; if the same figure is drawn twice, then all pixels are restored to their original values.
- Specified by:
setXORModein classjava.awt.Graphics- Parameters:
c1- the XOR alternation color
-
copyArea
public void copyArea(int x, int y, int width, int height, int dx, int dy)Copies an area of the component by a distance specified bydxanddy. From the point specified byxandy, this method copies downwards and to the right. To copy an area of the component to the left or upwards, specify a negative value fordxordy. If a portion of the source rectangle lies outside the bounds of the component, or is obscured by another window or component,copyAreawill be unable to copy the associated pixels. The area that is omitted can be refreshed by calling the component'spaintmethod.- Specified by:
copyAreain classjava.awt.Graphics- Parameters:
x- the x coordinate of the source rectangle.y- the y coordinate of the source rectangle.width- the width of the source rectangle.height- the height of the source rectangle.dx- the horizontal distance to copy the pixels.dy- the vertical distance to copy the pixels.
-
-