Package org.openpdf.renderer
Class PDFRenderer
java.lang.Object
org.openpdf.renderer.BaseWatchable
org.openpdf.renderer.PDFRenderer
This class turns a set of PDF Commands from a PDF page into an image. It
encapsulates the state of drawing in terms of stroke, fill, transform,
etc., as well as pushing and popping these states.
When the run method is called, this class goes through all remaining commands
in the PDF Page and draws them to its buffered image. It then updates any
ImageConsumers with the drawn data.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.openpdf.renderer.BaseWatchable
BaseWatchable.Gate -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intwhere we are in the page's command listprivate Graphics2Dthe graphics object for use within an iteration.private Rectangle2Dthe total region of this image that has been written toprivate final ImageInfothe info about the image, if we need to recreate itprivate WeakReference<BufferedImage>a weak reference to the image we render into.private GeneralPaththe last shape we drew (to check for overlaps)private AffineTransformstatic final intstatic final float[]static final intstatic final floatstatic final floatstatic final floatprivate final List<ImageObserver>the image observers that will be updated when this image changesprivate PDFPagethe page we were generate fromprivate Stack<PDFRenderer.GraphicsState>the stack of push()ed graphics statesprivate PDFRenderer.GraphicsStatethe current graphics stateprivate longthe next time the image should be notified about updatesprivate Rectangle2Dthe sum of all the individual dirty regions since the last updatestatic final longhow long (in milliseconds) to wait between image updatesFields inherited from interface org.openpdf.renderer.Watchable
COMPLETED, ERROR, NEEDS_DATA, NOT_STARTED, PAUSED, RUNNING, STOPPED, UNKNOWN -
Constructor Summary
ConstructorsConstructorDescriptionPDFRenderer(PDFPage page, Graphics2D g, Rectangle imgbounds, Rectangle2D clip, Color bgColor) create a new PDFGraphics state, given a Graphics2D.PDFRenderer(PDFPage page, ImageInfo imageinfo, BufferedImage bi) create a new PDFGraphics state -
Method Summary
Modifier and TypeMethodDescriptionprivate Rectangle2DaddDirtyRegion(Rectangle2D region, Rectangle2D glob) Append a rectangle to the total dirty region of this shapevoidaddObserver(ImageObserver observer) Add an image observerprivate BasicStrokeauto adjust the stroke width, according to 6.5.4, which presumes that the device characteristics (an image) require a single pixel wide line, even if the width is set to less.voidcleanup()Called when iteration has stoppedvoidclip(GeneralPath s) add the path to the current clip.voiddraw(GeneralPath p, BasicStroke bs) draw an outline.draw an image.fill(GeneralPath s) fill an outline using the current fill paintgetImage()If exists, returns the image which is used by the renderer.get the initial transform from page space to Java spaceGet the last shape drawnprivate BufferedImageConvert an image mask into an image by painting over any pixels that have a value in the image with the current paintget the current stroke as a BasicStrokeget the current affinetransformintiterate()Draws the next command in the PDFPage to the buffered image.private voidnotifyObservers(BufferedImage bi, Rectangle2D region) Notify the observer that a region of the image has changedvoidpop()restore the state of this object to what it was when the previous push() was called.voidpush()push the current graphics state onto the stack.voidRemember the current transformationvoidremoveObserver(ImageObserver observer) Remove an image observerprivate booleanDetermine if we are finishedprivate voidset the clip to be the given shape.voidsetFillAlpha(float alpha) set the stroke alphavoidsetFillPaint(PDFPaint paint) set the fill colorvoidsetLastShape(GeneralPath shape) Set the last shape drawnvoidsetStroke(BasicStroke bs) set the current stroke as a BasicStrokevoidsetStrokeAlpha(float alpha) set the stroke alphavoidsetStrokePaint(PDFPaint paint) set the stroke colorvoidsetStrokeParts(float w, int cap, int join, float limit, float[] ary, float phase) Set some or all aspects of the current stroke.voidreplace the current transform with the given one.voidsetup()Setup rendering.private voidSet up the graphics transform to match the clip region to the image size.private Rectangle2DsmartDrawImage(PDFImage image, BufferedImage bi, Rectangle r, AffineTransform at) stroke(GeneralPath s, boolean autoAdjustStroke) draw an outline using the current stroke and draw paintvoidconcatenate the given transform with the current transformMethods inherited from class org.openpdf.renderer.BaseWatchable
execute, getErrorHandler, getException, getStatus, go, go, go, go, isExecutable, isFinished, isSuppressSetErrorStackTrace, run, setError, setErrorHandler, setStatus, setSuppressSetErrorStackTrace, stop, waitForFinish
-
Field Details
-
page
the page we were generate from -
currentCommand
private int currentCommandwhere we are in the page's command list -
imageRef
a weak reference to the image we render into. For the image to remain available, some other code must retain a strong reference to it. -
g
the graphics object for use within an iteration. Note this must be set to null at the end of each iteration, or the image will not be collected -
state
the current graphics state -
stack
the stack of push()ed graphics states -
globalDirtyRegion
the total region of this image that has been written to -
observers
the image observers that will be updated when this image changes -
lastShape
the last shape we drew (to check for overlaps) -
lastTransform
-
imageinfo
the info about the image, if we need to recreate it -
then
private long thenthe next time the image should be notified about updates -
unupdatedRegion
the sum of all the individual dirty regions since the last update -
UPDATE_DURATION
public static final long UPDATE_DURATIONhow long (in milliseconds) to wait between image updates- See Also:
-
NOPHASE
public static final float NOPHASE- See Also:
-
NOWIDTH
public static final float NOWIDTH- See Also:
-
NOLIMIT
public static final float NOLIMIT- See Also:
-
NOCAP
public static final int NOCAP- See Also:
-
NODASH
public static final float[] NODASH -
NOJOIN
public static final int NOJOIN- See Also:
-
-
Constructor Details
-
PDFRenderer
create a new PDFGraphics state- Parameters:
page- the current pageimageinfo- the paramters of the image to render
-
PDFRenderer
public PDFRenderer(PDFPage page, Graphics2D g, Rectangle imgbounds, Rectangle2D clip, Color bgColor) create a new PDFGraphics state, given a Graphics2D. This version will not create an image, and you will get a NullPointerException if you attempt to call getImage().- Parameters:
page- the current pageg- the Graphics2D object to use for drawingimgbounds- the bounds of the image into which to fit the pageclip- the portion of the page to draw, in page space, or null if the whole page should be drawnbgColor- the color to draw the background of the image, or null for no color (0 alpha value)
-
-
Method Details
-
setupRendering
Set up the graphics transform to match the clip region to the image size. -
push
public void push()push the current graphics state onto the stack. Continue working with the current object; calling pop() restores the state of this object to its state when push() was called. -
pop
public void pop()restore the state of this object to what it was when the previous push() was called. -
stroke
draw an outline using the current stroke and draw paint- Parameters:
s- the path to stroke- Returns:
- a Rectangle2D to which the current region being drawn will be added. May also be null, in which case no dirty region will be recorded.
-
autoAdjustStrokeWidth
auto adjust the stroke width, according to 6.5.4, which presumes that the device characteristics (an image) require a single pixel wide line, even if the width is set to less. We determine the scaling to see if we would produce a line that was too small, and if so, scale it up to produce a graphics line of 1 pixel, or so. This matches our output with Adobe Reader.- Parameters:
g-bs-- Returns:
-
draw
draw an outline.- Parameters:
p- the path to drawbs- the stroke with which to draw the path
-
fill
fill an outline using the current fill paint- Parameters:
s- the path to fill
-
drawImage
draw an image.- Parameters:
image- the image to draw
-
smartDrawImage
private Rectangle2D smartDrawImage(PDFImage image, BufferedImage bi, Rectangle r, AffineTransform at) -
clip
add the path to the current clip. The new clip will be the intersection of the old clip and given path. -
setClip
set the clip to be the given shape. The current clip is not taken into account. -
getTransform
get the current affinetransform -
transform
concatenate the given transform with the current transform -
setTransform
replace the current transform with the given one. -
getInitialTransform
get the initial transform from page space to Java space -
setStrokeParts
public void setStrokeParts(float w, int cap, int join, float limit, float[] ary, float phase) Set some or all aspects of the current stroke.- Parameters:
w- the width of the stroke, or NOWIDTH to leave it unchangedcap- the end cap style, or NOCAP to leave it unchangedjoin- the join style, or NOJOIN to leave it unchangedlimit- the miter limit, or NOLIMIT to leave it unchangedphase- the phase of the dash array, or NOPHASE to leave it unchangedary- the dash array, or null to leave it unchanged. phase and ary must both be valid, or phase must be NOPHASE while ary is null.
-
getStroke
get the current stroke as a BasicStroke -
setStroke
set the current stroke as a BasicStroke -
setStrokePaint
set the stroke color -
setFillPaint
set the fill color -
setStrokeAlpha
public void setStrokeAlpha(float alpha) set the stroke alpha -
setFillAlpha
public void setFillAlpha(float alpha) set the stroke alpha -
addObserver
Add an image observer -
removeObserver
Remove an image observer -
setLastShape
Set the last shape drawn -
getLastShape
Get the last shape drawn -
getImage
If exists, returns the image which is used by the renderer.- Returns:
- a BufferedImage or null
-
setup
public void setup()Setup rendering. Called before iteration begins- Overrides:
setupin classBaseWatchable
-
iterate
Draws the next command in the PDFPage to the buffered image. The image will be notified about changes no less than every UPDATE_DURATION milliseconds.- Specified by:
iteratein classBaseWatchable- Returns:
- Watchable.RUNNING when there are commands to be processed
- Watchable.NEEDS_DATA when there are no commands to be processed, but the page is not yet complete
- Watchable.COMPLETED when the page is done and all the commands have been processed
- Watchable.STOPPED if the image we are rendering into has gone away
- Throws:
Exception
-
cleanup
public void cleanup()Called when iteration has stopped- Overrides:
cleanupin classBaseWatchable
-
addDirtyRegion
Append a rectangle to the total dirty region of this shape -
rendererFinished
private boolean rendererFinished()Determine if we are finished -
notifyObservers
Notify the observer that a region of the image has changed -
getMaskedImage
Convert an image mask into an image by painting over any pixels that have a value in the image with the current paint -
getLastTransform
- Returns:
- Returns the lastTransform.
-
rememberTransformation
public void rememberTransformation()Remember the current transformation
-