fop 0.93

org.apache.fop.svg
Class PDFGraphics2D

java.lang.Object
  extended byjava.awt.Graphics
      extended byjava.awt.Graphics2D
          extended byorg.apache.xmlgraphics.java2d.AbstractGraphics2D
              extended byorg.apache.fop.svg.PDFGraphics2D
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
PDFDocumentGraphics2D

public class PDFGraphics2D
extends org.apache.xmlgraphics.java2d.AbstractGraphics2D

PDF Graphics 2D. Used for drawing into a pdf document as if it is a graphics object. This takes a pdf document and draws into it.

Version:
$Id: PDFGraphics2D.java 474225 2006-11-13 10:08:19Z jeremias $
Author:
Keiron Liddle
See Also:
org.apache.batik.ext.awt.g2d.AbstractGraphics2D

Field Summary
protected  int baseLevel
          The PDF graphics state level that this svg is being drawn into.
protected  java.lang.String currentFontName
          the current (internal) font name
protected  float currentFontSize
          the current font size in millipoints
protected  java.io.StringWriter currentStream
          the current stream to add PDF commands to
protected  FontInfo fontInfo
          The current font information.
protected  PDFState graphicsState
          the current state of the pdf graphics
protected  int[] jpegCount
          The count of JPEG images added to document so they recieve unique keys.
protected  java.io.OutputStream outputStream
          The output stream for the pdf document.
protected  Font ovFontState
          The override font state used when drawing text and the font cannot be set using java fonts.
protected  java.lang.String pageRef
          The PDF reference of the current page.
protected  PDFDocument pdfDoc
          the PDF Document being created
protected  PDFResourceContext resourceContext
          The current resource context for adding fonts, patterns etc.
 
Fields inherited from class org.apache.xmlgraphics.java2d.AbstractGraphics2D
gc, inPossibleRecursion, textAsShapes
 
Constructor Summary
protected PDFGraphics2D(boolean textAsShapes)
          Create a new PDFGraphics2D.
  PDFGraphics2D(boolean textAsShapes, FontInfo fi, PDFDocument doc, PDFResourceContext page, java.lang.String pref, java.lang.String font, float size)
          Create a new PDFGraphics2D with the given pdf document info.
  PDFGraphics2D(PDFGraphics2D g)
          This constructor supports the create method.
 
Method Summary
 void addJpegImage(JpegImage jpeg, float x, float y, float width, float height)
          Add a JPEG image directly to the PDF document.
 void addLink(java.awt.geom.Rectangle2D bounds, java.awt.geom.AffineTransform trans, java.lang.String dest, int linkType)
          This is a pdf specific method used to add a link to the pdf document.
protected  void applyColor(java.awt.Color col, boolean fill)
          Apply the java Color to PDF.
protected  boolean applyPaint(java.awt.Paint paint, boolean fill)
          Apply the java paint to the PDF.
protected  void applyStroke(java.awt.Stroke stroke)
          Apply the stroke to the PDF.
protected  boolean applyUnknownPaint(java.awt.Paint paint, java.awt.Shape shape)
           
protected  void checkTransparencyAllowed()
          Checks whether the use of transparency is allowed.
 void copyArea(int x, int y, int width, int height, int dx, int dy)
          Copies an area of the component by a distance specified by dx and dy.
 java.awt.Graphics create()
          Creates a new Graphics object that is a copy of this Graphics object.
 void dispose()
          Disposes of this graphics context and releases any system resources that it is using.
protected  void doDrawing(boolean fill, boolean stroke, boolean nonzero)
          Do the PDF drawing command.
 void draw(java.awt.Shape s)
          Strokes the outline of a Shape using the settings of the current Graphics2D context.
 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.
 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.
 void drawRenderableImage(java.awt.image.renderable.RenderableImage img, java.awt.geom.AffineTransform xform)
          Renders a RenderableImage, applying a transform from image space into user space before drawing.
 void drawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)
          Renders a RenderedImage, applying a transform from image space into user space before drawing.
 void drawString(java.text.AttributedCharacterIterator iterator, float x, float y)
          Renders the text of the specified iterator, using the Graphics2D context's current Paint.
 void drawString(java.lang.String s, float x, float y)
          Renders the text specified by the specified String, using the current Font and Paint attributes in the Graphics2D context.
 void fill(java.awt.Shape s)
          Fills the interior of a Shape using the settings of the Graphics2D context.
protected  java.awt.geom.AffineTransform getBaseTransform()
          This is mainly used for shading patterns which use the document-global coordinate system instead of the local one.
 java.lang.StringBuffer getBuffer()
          Get the string buffer from the currentStream, containing all the commands written into this Grpahics so far.
 java.awt.GraphicsConfiguration getDeviceConfiguration()
          Returns the device configuration associated with this Graphics2D.
 java.awt.FontMetrics getFontMetrics(java.awt.Font f)
          Gets the font metrics for the specified font.
 java.lang.String getString()
          Get the string containing all the commands written into this Grpahics.
protected  void handleIOException(java.io.IOException ioe)
          Central handler for IOExceptions for this class.
protected  void preparePainting()
          This method is used by PDFDocumentGraphics2D to prepare a new page if necessary.
 void processPathIterator(java.awt.geom.PathIterator iter)
          Processes a path iterator generating the necessary painting operations.
 void setGraphicContext(org.apache.xmlgraphics.java2d.GraphicContext c)
          Set the Grpahics context.
 void setOutputStream(java.io.OutputStream os)
          Set the output stream that this PDF document is being drawn to.
 void setOverrideFontState(Font infont)
          Set the override font state for drawing text.
 void setPDFState(PDFState state)
          Set the PDF state to use when starting to draw into the PDF graphics.
 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.
protected  void writeClip(java.awt.Shape s)
          Set the clipping shape for future PDF drawing in the current graphics state.
 
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, 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
 
Methods inherited from class java.awt.Graphics2D
draw3DRect, fill3DRect
 
Methods inherited from class java.awt.Graphics
create, drawBytes, drawChars, drawPolygon, fillPolygon, finalize, getClipBounds, getClipRect, getFontMetrics, hitClip, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

pdfDoc

protected PDFDocument pdfDoc
the PDF Document being created


resourceContext

protected PDFResourceContext resourceContext
The current resource context for adding fonts, patterns etc.


pageRef

protected java.lang.String pageRef
The PDF reference of the current page.


graphicsState

protected PDFState graphicsState
the current state of the pdf graphics


baseLevel

protected int baseLevel
The PDF graphics state level that this svg is being drawn into.


jpegCount

protected int[] jpegCount
The count of JPEG images added to document so they recieve unique keys.


fontInfo

protected FontInfo fontInfo
The current font information.


ovFontState

protected Font ovFontState
The override font state used when drawing text and the font cannot be set using java fonts.


currentStream

protected java.io.StringWriter currentStream
the current stream to add PDF commands to


currentFontName

protected java.lang.String currentFontName
the current (internal) font name


currentFontSize

protected float currentFontSize
the current font size in millipoints


outputStream

protected java.io.OutputStream outputStream
The output stream for the pdf document. If this is set then it can progressively output the pdf document objects to reduce memory. Especially with images.

Constructor Detail

PDFGraphics2D

public PDFGraphics2D(boolean textAsShapes,
                     FontInfo fi,
                     PDFDocument doc,
                     PDFResourceContext page,
                     java.lang.String pref,
                     java.lang.String font,
                     float size)
Create a new PDFGraphics2D with the given pdf document info. This is used to create a Graphics object for use inside an already existing document.

Parameters:
textAsShapes - if true then draw text as shapes
fi - the current font information
doc - the pdf document for creating pdf objects
page - the current resource context or page
pref - the PDF reference of the current page
font - the current font name
size - the current font size

PDFGraphics2D

protected PDFGraphics2D(boolean textAsShapes)
Create a new PDFGraphics2D.

Parameters:
textAsShapes - true if drawing text as shapes

PDFGraphics2D

public PDFGraphics2D(PDFGraphics2D g)
This constructor supports the create method. This is not implemented properly.

Parameters:
g - the PDF graphics to make a copy of
Method Detail

create

public java.awt.Graphics create()
Creates a new Graphics object that is a copy of this Graphics object.

Returns:
a new graphics context that is a copy of this graphics context.

handleIOException

protected void handleIOException(java.io.IOException ioe)
Central handler for IOExceptions for this class.

Parameters:
ioe - IOException to handle

preparePainting

protected void preparePainting()
This method is used by PDFDocumentGraphics2D to prepare a new page if necessary.


setPDFState

public void setPDFState(PDFState state)
Set the PDF state to use when starting to draw into the PDF graphics.

Parameters:
state - the PDF state

setOutputStream

public void setOutputStream(java.io.OutputStream os)
Set the output stream that this PDF document is being drawn to. This is so that it can progressively use the PDF document to output data such as images. This results in a significant saving on memory.

Parameters:
os - the output stream that is being used for the PDF document

getString

public java.lang.String getString()
Get the string containing all the commands written into this Grpahics.

Returns:
the string containing the PDF markup

getBuffer

public java.lang.StringBuffer getBuffer()
Get the string buffer from the currentStream, containing all the commands written into this Grpahics so far.

Returns:
the StringBuffer containing the PDF markup

setGraphicContext

public void setGraphicContext(org.apache.xmlgraphics.java2d.GraphicContext c)
Set the Grpahics context.

Parameters:
c - the graphics context to use

setOverrideFontState

public void setOverrideFontState(Font infont)
Set the override font state for drawing text. This is used by the PDF text painter so that it can temporarily set the font state when a java font cannot be used. The next drawString will use this font state.

Parameters:
infont - the font state to use

getBaseTransform

protected java.awt.geom.AffineTransform getBaseTransform()
This is mainly used for shading patterns which use the document-global coordinate system instead of the local one.

Returns:
the transformation matrix that established the basic user space for this document

addLink

public void addLink(java.awt.geom.Rectangle2D bounds,
                    java.awt.geom.AffineTransform trans,
                    java.lang.String dest,
                    int linkType)
This is a pdf specific method used to add a link to the pdf document.

Parameters:
bounds - the bounds of the link in user coordinates
trans - the transform of the current drawing position
dest - the PDF destination
linkType - the type of link, internal or external

addJpegImage

public void addJpegImage(JpegImage jpeg,
                         float x,
                         float y,
                         float width,
                         float height)
Add a JPEG image directly to the PDF document. This is used by the PDFImageElementBridge to draw a JPEG directly into the pdf document rather than converting the image into a bitmap and increasing the size.

Parameters:
jpeg - the jpeg image to draw
x - the x position
y - the y position
width - the width to draw the image
height - the height to draw the image

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 (xy) 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 drawImage returns false. As more of the image becomes available, the process that draws the image notifies the specified image observer.

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 was drawn
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,
                         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 drawImage returns false. As more of the image becomes available, the process that draws the image notifies the image observer by calling its imageUpdate method.

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.

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 was 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. A Graphics object cannot be used after disposehas been called.

When a Java program runs, a large number of Graphics objects 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 paint and update methods of components are automatically released by the system when those methods return. For efficiency, programmers should call dispose when finished using a Graphics object only if it was created directly from a component or another Graphics object.

See Also:
Graphics.finalize(), Component.paint(java.awt.Graphics), Component.update(java.awt.Graphics), Component.getGraphics(), Graphics.create()

draw

public void draw(java.awt.Shape s)
Strokes the outline of a Shape using the settings of the current Graphics2D context. The rendering attributes applied include the Clip, Transform, Paint, Composite and Stroke attributes.

Parameters:
s - the Shape to 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)

writeClip

protected void writeClip(java.awt.Shape s)
Set the clipping shape for future PDF drawing in the current graphics state. This sets creates and writes a clipping shape that will apply to future drawings in the current graphics state.

Parameters:
s - the clipping shape

applyColor

protected void applyColor(java.awt.Color col,
                          boolean fill)
Apply the java Color to PDF. This converts the java colour to a PDF colour and sets it for the next drawing.

Parameters:
col - the java colour
fill - true if the colour will be used for filling

applyPaint

protected boolean applyPaint(java.awt.Paint paint,
                             boolean fill)
Apply the java paint to the PDF. This takes the java paint sets up the appropraite PDF commands for the drawing with that paint. Currently this supports the gradients and patterns from batik.

Parameters:
paint - the paint to convert to PDF
fill - true if the paint should be set for filling
Returns:
true if the paint is handled natively, false if the paint should be rasterized

applyUnknownPaint

protected boolean applyUnknownPaint(java.awt.Paint paint,
                                    java.awt.Shape shape)

applyStroke

protected void applyStroke(java.awt.Stroke stroke)
Apply the stroke to the PDF. This takes the java stroke and outputs the appropriate settings to the PDF so that the stroke attributes are handled.

Parameters:
stroke - the java stroke

drawRenderedImage

public void drawRenderedImage(java.awt.image.RenderedImage img,
                              java.awt.geom.AffineTransform xform)
Renders a RenderedImage, applying a transform from image space into user space before drawing. The transformation from user space into device space is done with the current Transform in the Graphics2D. The specified transformation is applied to the image before the transform attribute in the Graphics2D context is applied. The rendering attributes applied include the Clip, Transform, and Composite attributes. Note that no rendering is done if the specified transform is noninvertible.

Parameters:
img - the image to be rendered
xform - 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 a RenderableImage, applying a transform from image space into user space before drawing. The transformation from user space into device space is done with the current Transform in the Graphics2D. The specified transformation is applied to the image before the transform attribute in the Graphics2D context is applied. The rendering attributes applied include the Clip, Transform, and Composite attributes. Note that no rendering is done if the specified transform is noninvertible.

Rendering hints set on the Graphics2D object might be used in rendering the RenderableImage. If explicit control is required over specific hints recognized by a specific RenderableImage, or if knowledge of which hints are used is required, then a RenderedImage should be obtained directly from the RenderableImage and rendered using drawRenderedImage.

Parameters:
img - the image to be rendered
xform - 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)

drawString

public void drawString(java.lang.String s,
                       float x,
                       float y)
Renders the text specified by the specified String, using the current Font and Paint attributes in the Graphics2D context. The baseline of the first character is at position (xy) in the User Space. The rendering attributes applied include the Clip, Transform, Paint, Font and Composite attributes. 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.

Parameters:
s - the String to be rendered
x - the coordinate where the String should be rendered
y - the coordinate where the String should 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)

drawString

public void drawString(java.text.AttributedCharacterIterator iterator,
                       float x,
                       float y)
Renders the text of the specified iterator, using the Graphics2D context's current Paint. The iterator must specify a font for each character. The baseline of the first character is at position (xy) in the User Space. The rendering attributes applied include the Clip, Transform, Paint, and Composite attributes. 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.

Parameters:
iterator - the iterator whose text is to be rendered
x - the coordinate where the iterator's text is to be rendered
y - the coordinate where the iterator's text is to be rendered
See Also:
AbstractGraphics2D.setPaint(java.awt.Paint), Graphics.setColor(java.awt.Color), 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 a Shape using the settings of the Graphics2D context. The rendering attributes applied include the Clip, Transform, Paint, and Composite.

Parameters:
s - the Shape to 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)

checkTransparencyAllowed

protected void checkTransparencyAllowed()
Checks whether the use of transparency is allowed.


processPathIterator

public void processPathIterator(java.awt.geom.PathIterator iter)
Processes a path iterator generating the necessary painting operations.

Parameters:
iter - PathIterator to process

doDrawing

protected void doDrawing(boolean fill,
                         boolean stroke,
                         boolean nonzero)
Do the PDF drawing command. This does the PDF drawing command according to fill stroke and winding rule.

Parameters:
fill - true if filling the path
stroke - true if stroking the path
nonzero - true if using the non-zero winding rule

getDeviceConfiguration

public java.awt.GraphicsConfiguration getDeviceConfiguration()
Returns the device configuration associated with this Graphics2D.

Returns:
the PDF graphics configuration

getFontMetrics

public java.awt.FontMetrics getFontMetrics(java.awt.Font f)
Gets the font metrics for the specified font.

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.

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 by dx and dy. From the point specified by x and y, this method copies downwards and to the right. To copy an area of the component to the left or upwards, specify a negative value for dx or dy. If a portion of the source rectangle lies outside the bounds of the component, or is obscured by another window or component, copyArea will be unable to copy the associated pixels. The area that is omitted can be refreshed by calling the component's paint method.

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.

fop 0.93

Copyright 1999-2006 The Apache Software Foundation. All Rights Reserved.