Class Java2DRenderer

All Implemented Interfaces:
Printable, Constants, Renderer
Direct Known Subclasses:
AWTRenderer, PageableRenderer, PNGRenderer, TIFFRenderer

public abstract class Java2DRenderer extends AbstractPathOrientedRenderer implements Printable
The Java2DRenderer class provides the abstract technical foundation for all rendering with the Java2D API. Renderers like AWTRenderer subclass it and provide the concrete output paths.

A lot of the logic is performed by AbstractRenderer. The class-variables currentIPPosition and currentBPPosition hold the position of the currently rendered area.

Java2DGraphicsState state holds the Graphics2D, which is used along the whole rendering. state also acts as a stack (state.push() and state.pop()).

The rendering process is basically always the same:

void renderXXXXX(Area area) { //calculate the currentPosition state.updateFont(name, size, null); state.updateColor(ct, false, null); state.getGraph.draw(new Shape(args)); }

  • Field Details

    • scaleFactor

      protected double scaleFactor
      The scale factor for the image size, values: ]0 ; 1]
    • pageWidth

      protected int pageWidth
      The page width in pixels
    • pageHeight

      protected int pageHeight
      The page height in pixels
    • pageViewportList

      protected List pageViewportList
      List of Viewports
    • antialiasing

      protected boolean antialiasing
      true if anti-aliasing is set
    • qualityRendering

      protected boolean qualityRendering
      true if qualityRendering is set
    • transparentPageBackground

      protected boolean transparentPageBackground
      false: paints a non-transparent white background, true: for a transparent background
    • state

      protected Java2DGraphicsState state
      The current state, holds a Graphics2D and its context
  • Constructor Details

    • Java2DRenderer

      public Java2DRenderer(FOUserAgent userAgent)
      Default constructor
      Parameters:
      userAgent - the user agent that contains configuration details. This cannot be null.
  • Method Details

    • getUserAgent

      public FOUserAgent getUserAgent()
      Description copied from class: AbstractRenderer
      Returns the associated user agent.
      Specified by:
      getUserAgent in interface Renderer
      Overrides:
      getUserAgent in class AbstractRenderer
      Returns:
      the FOUserAgent
    • setupFontInfo

      public void setupFontInfo(FontInfo inFontInfo)
      Set up the given FontInfo.
      Specified by:
      setupFontInfo in interface Renderer
      Overrides:
      setupFontInfo in class PrintRenderer
      Parameters:
      inFontInfo - The font information
    • getGraphics2DAdapter

      public Graphics2DAdapter getGraphics2DAdapter()
      Specified by:
      getGraphics2DAdapter in interface Renderer
      Overrides:
      getGraphics2DAdapter in class AbstractRenderer
      Returns:
      the adapter for painting Java2D images (or null if not supported)
    • setScaleFactor

      public void setScaleFactor(double newScaleFactor)
      Sets the new scale factor.
      Parameters:
      newScaleFactor - ]0 ; 1]
    • getScaleFactor

      public double getScaleFactor()
      Returns:
      the scale factor
    • startRenderer

      public void startRenderer(OutputStream out) throws IOException
      Initiates the rendering phase. This must only be called once for a rendering. If stopRenderer is called then this may be called again for a new document rendering.
      Specified by:
      startRenderer in interface Renderer
      Overrides:
      startRenderer in class AbstractRenderer
      Parameters:
      out - The OutputStream to use for output
      Throws:
      IOException - If an I/O error occurs
    • stopRenderer

      public void stopRenderer() throws IOException
      Signals the end of the rendering phase. The renderer should reset to an initial state and dispose of any resources for the completed rendering.
      Specified by:
      stopRenderer in interface Renderer
      Overrides:
      stopRenderer in class AbstractRenderer
      Throws:
      IOException - If an I/O error occurs
    • isRenderingDone

      public boolean isRenderingDone()
      Returns:
      true if the renderer is not currently processing
    • getCurrentPageNumber

      public int getCurrentPageNumber()
      Returns:
      The 0-based current page number
    • setCurrentPageNumber

      public void setCurrentPageNumber(int c)
      Parameters:
      c - the 0-based current page number
    • getNumberOfPages

      public int getNumberOfPages()
      Returns the number of pages available. This method is also part of the Pageable interface.
      Returns:
      The 0-based total number of rendered pages
      See Also:
    • clearViewportList

      public void clearViewportList()
      Clears the ViewportList. Used if the document is reloaded.
    • renderPage

      public void renderPage(PageViewport pageViewport) throws IOException, FOPException
      This method override only stores the PageViewport in a List. No actual rendering is performed here. A renderer override renderPage() to get the freshly produced PageViewport, and render them on the fly (producing the desired BufferedImages by calling getPageImage(), which lazily starts the rendering process).
      Specified by:
      renderPage in interface Renderer
      Overrides:
      renderPage in class AbstractRenderer
      Parameters:
      pageViewport - the PageViewport object supplied by the Area Tree
      Throws:
      IOException - In case of an I/O error
      FOPException - if cloning of pageViewport is not supported
      See Also:
    • rememberPage

      protected void rememberPage(PageViewport pageViewport)
      Stores the pageViewport in a list of page viewports so they can be rendered later. Subclasses can override this method to filter pages, for example.
      Parameters:
      pageViewport - the page viewport
    • getPageImage

      public BufferedImage getPageImage(PageViewport pageViewport)
      Generates a desired page from the renderer's page viewport list.
      Parameters:
      pageViewport - the PageViewport to be rendered
      Returns:
      the java.awt.image.BufferedImage corresponding to the page or null if the page doesn't exist.
    • getBufferedImage

      protected BufferedImage getBufferedImage(int bitmapWidth, int bitmapHeight)
      Returns a specific BufferedImage to paint a page image on. This method can be overridden in subclasses to produce different image formats (ex. grayscale or b/w).
      Parameters:
      bitmapWidth - width of the image in pixels
      bitmapHeight - heigth of the image in pixels
      Returns:
      the newly created BufferedImage
    • getPageViewport

      public PageViewport getPageViewport(int pageIndex) throws FOPException
      Returns a page viewport.
      Parameters:
      pageIndex - the page index (zero-based)
      Returns:
      the requested PageViewport instance
      Throws:
      FOPException - If the page is out of range.
    • getPageImage

      public BufferedImage getPageImage(int pageNum) throws FOPException
      Generates a desired page from the renderer's page viewport list.
      Parameters:
      pageNum - the 0-based page number to generate
      Returns:
      the java.awt.image.BufferedImage corresponding to the page or null if the page doesn't exist.
      Throws:
      FOPException - If there's a problem preparing the page image
    • saveGraphicsState

      protected void saveGraphicsState()
      Saves the graphics state of the rendering engine.
      Specified by:
      saveGraphicsState in class AbstractPathOrientedRenderer
    • restoreGraphicsState

      protected void restoreGraphicsState()
      Restores the last graphics state of the rendering engine.
      Specified by:
      restoreGraphicsState in class AbstractPathOrientedRenderer
    • concatenateTransformationMatrix

      protected void concatenateTransformationMatrix(AffineTransform at)
      Concatenates the current transformation matrix with the given one, therefore establishing a new coordinate system.
      Specified by:
      concatenateTransformationMatrix in class AbstractPathOrientedRenderer
      Parameters:
      at - the transformation matrix to process (coordinates in points)
    • startVParea

      protected void startVParea(CTM ctm, Rectangle clippingRect)
      Establishes a new viewport area.
      Specified by:
      startVParea in class AbstractRenderer
      Parameters:
      ctm - the coordinate transformation matrix to use
      clippingRect - the clipping rectangle if the viewport should be clipping, null if no clipping is performed.
    • endVParea

      protected void endVParea()
      Signals exit from a viewport area. Subclasses can restore transformation matrices valid before the viewport area was started.
      Specified by:
      endVParea in class AbstractRenderer
    • startLayer

      protected void startLayer(String layer)
      Establish new optional content group layer.
      Specified by:
      startLayer in class AbstractRenderer
      Parameters:
      layer - name of layer
    • endLayer

      protected void endLayer()
      Finish current optional content group layer.
      Specified by:
      endLayer in class AbstractRenderer
    • breakOutOfStateStack

      protected List breakOutOfStateStack()
      Breaks out of the state stack to handle fixed block-containers.
      Specified by:
      breakOutOfStateStack in class AbstractPathOrientedRenderer
      Returns:
      the saved state stack to recreate later
    • restoreStateStackAfterBreakOut

      protected void restoreStateStackAfterBreakOut(List breakOutList)
      Restores the state stack after a break out.
      Specified by:
      restoreStateStackAfterBreakOut in class AbstractPathOrientedRenderer
      Parameters:
      breakOutList - the state stack to restore.
    • updateColor

      protected void updateColor(Color col, boolean fill)
      Establishes a new foreground or fill color.
      Specified by:
      updateColor in class AbstractPathOrientedRenderer
      Parameters:
      col - the color to apply (null skips this operation)
      fill - true to set the fill color, false for the foreground color
    • clip

      protected void clip()
      Clip using the current path.
      Specified by:
      clip in class AbstractPathOrientedRenderer
    • closePath

      protected void closePath()
      Closes the current subpath by appending a straight line segment from the current point to the starting point of the subpath.
      Specified by:
      closePath in class AbstractPathOrientedRenderer
    • lineTo

      protected void lineTo(float x, float y)
      Appends a straight line segment from the current point to (x, y). The new current point is (x, y).
      Specified by:
      lineTo in class AbstractPathOrientedRenderer
      Parameters:
      x - x coordinate
      y - y coordinate
    • moveTo

      protected void moveTo(float x, float y)
      Moves the current point to (x, y), omitting any connecting line segment.
      Specified by:
      moveTo in class AbstractPathOrientedRenderer
      Parameters:
      x - x coordinate
      y - y coordinate
    • clipRect

      protected void clipRect(float x, float y, float width, float height)
      Clip using a rectangular area.
      Specified by:
      clipRect in class AbstractPathOrientedRenderer
      Parameters:
      x - the x coordinate (in points)
      y - the y coordinate (in points)
      width - the width of the rectangle (in points)
      height - the height of the rectangle (in points)
    • fillRect

      protected void fillRect(float x, float y, float width, float height)
      Fill a rectangular area.
      Specified by:
      fillRect in class AbstractPathOrientedRenderer
      Parameters:
      x - the x coordinate
      y - the y coordinate
      width - the width of the rectangle
      height - the height of the rectangle
    • drawBorderLine

      protected void drawBorderLine(float x1, float y1, float x2, float y2, boolean horz, boolean startOrBefore, int style, Color col)
      Draw a border segment of an XSL-FO style border.
      Specified by:
      drawBorderLine in class AbstractPathOrientedRenderer
      Parameters:
      x1 - starting x coordinate
      y1 - starting y coordinate
      x2 - ending x coordinate
      y2 - ending y coordinate
      horz - true for horizontal border segments, false for vertical border segments
      startOrBefore - true for border segments on the start or before edge, false for end or after.
      style - the border style (one of Constants.EN_DASHED etc.)
      col - the color for the border segment
    • drawBorderLine

      public static void drawBorderLine(Rectangle2D.Float lineRect, boolean horz, boolean startOrBefore, int style, Color col, Graphics2D g2d)
      Draw a border segment of an XSL-FO style border.
      Parameters:
      lineRect - the line defined by its bounding rectangle
      horz - true for horizontal border segments, false for vertical border segments
      startOrBefore - true for border segments on the start or before edge, false for end or after.
      style - the border style (one of Constants.EN_DASHED etc.)
      col - the color for the border segment
      g2d - the Graphics2D instance to paint to
    • renderText

      public void renderText(TextArea text)
      Render the given TextArea.
      Overrides:
      renderText in class AbstractRenderer
      Parameters:
      text - the text to render
    • renderText

      public static void renderText(TextArea text, Graphics2D g2d, Font font, FontInfo fontInfo)
      Renders a TextArea to a Graphics2D instance. Adjust the coordinate system so that the start of the baseline of the first character is at coordinate (0,0).
      Parameters:
      text - the TextArea
      g2d - the Graphics2D to render to
      font - the font to paint with
      fontInfo - the font information
    • renderLeader

      public void renderLeader(Leader area)
      Render leader area. This renders a leader area which is an area with a rule.
      Overrides:
      renderLeader in class AbstractRenderer
      Parameters:
      area - the leader area to render
    • renderImage

      public void renderImage(Image image, Rectangle2D pos)
      Renders an image area.
      Overrides:
      renderImage in class AbstractRenderer
      Parameters:
      image - The image
      pos - The target position of the image (todo) Make renderImage() protected
    • drawImage

      protected void drawImage(String uri, Rectangle2D pos, Map foreignAttributes)
      Draw an image at the indicated location.
      Specified by:
      drawImage in class AbstractPathOrientedRenderer
      Parameters:
      uri - the URI/URL of the image
      pos - the position of the image
      foreignAttributes - an optional Map with foreign attributes, may be null
    • createRendererContext

      protected RendererContext createRendererContext(int x, int y, int width, int height, Map foreignAttributes)
      Creates a RendererContext for an image.
      Overrides:
      createRendererContext in class PrintRenderer
      Parameters:
      x - the x coordinate (in millipoints)
      y - the y coordinate (in millipoints)
      width - the width of the image (in millipoints)
      height - the height of the image (in millipoints)
      foreignAttributes - a Map or foreign attributes, may be null
      Returns:
      the RendererContext
    • print

      public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
      Specified by:
      print in interface Printable
      Throws:
      PrinterException
    • beginTextObject

      protected void beginTextObject()
      Indicates the beginning of a text object.
      Specified by:
      beginTextObject in class AbstractPathOrientedRenderer
    • endTextObject

      protected void endTextObject()
      Indicates the end of a text object.
      Specified by:
      endTextObject in class AbstractPathOrientedRenderer
    • setTransparentPageBackground

      public void setTransparentPageBackground(boolean transparentPageBackground)
      Controls the page background.
      Parameters:
      transparentPageBackground - true if the background should be transparent