Class Java2DRenderer

java.lang.Object
org.xhtmlrenderer.swing.Java2DRenderer

public class Java2DRenderer extends Object

Renders an XML files, formatted with CSS, as an image. Input is a document in the form of file or URL, and output is a BufferedImage. A Java2DRenderer is not intended to be re-used for multiple document sources; just create new Java2DRenderers for each one you need. Java2DRenderer is not thread-safe. Standard usage pattern is

 File xhtml = ...
 Java2DRenderer rend = new Java2DRenderer(xhtml);
 BufferedImage image = rend.getImage();
 

The document is not loaded, and layout and render don't take place, until getImage() is called. Subsequent calls to getImage() don't result in a reload; create a new Java2DRenderer instance to do so.

As with RootPanel, you can access the SharedContext instance that will be used by this renderer and change settings to control the rendering process; use getSharedContext().

By default, this renderer will render to an RGB image which does not support transparency. To use another type of BufferedImage, either set the image type in constructor before calling getImage(), or else override the createBufferedImage(int, int) to have full control over the image we render to.

Not thread-safe.

  • Field Details

    • DEFAULT_HEIGHT

      private static final int DEFAULT_HEIGHT
      See Also:
    • DEFAULT_DOTS_PER_POINT

      private static final int DEFAULT_DOTS_PER_POINT
      See Also:
    • DEFAULT_DOTS_PER_PIXEL

      private static final int DEFAULT_DOTS_PER_PIXEL
      See Also:
    • DEFAULT_IMAGE_TYPE

      private static final int DEFAULT_IMAGE_TYPE
      See Also:
    • sharedContext

      private final SharedContext sharedContext
    • outputDevice

      private Java2DOutputDevice outputDevice
    • doc

      private @Nullable Document doc
    • root

      private @Nullable Box root
    • outputImage

      private BufferedImage outputImage
    • bufferedImageType

      private final int bufferedImageType
    • rendered

      private boolean rendered
      Whether we've completed rendering; image will only be rendered once.
    • sourceDocument

      private @Nullable String sourceDocument
    • sourceDocumentBase

      private @Nullable String sourceDocumentBase
    • width

      private final int width
    • height

      private final int height
    • NO_HEIGHT

      private static final int NO_HEIGHT
      See Also:
  • Constructor Details

    • Java2DRenderer

      public Java2DRenderer(String url, String baseUrl, int width, int height)
    • Java2DRenderer

      public Java2DRenderer(String url, String baseUrl, int width, int height, int bufferedImageType)
      Creates a new instance for a given URL. Does not render until getImage() is called for the first time.
      Parameters:
      url - The location of the document to be rendered.
      baseUrl - The base url for the document, against which relative paths are resolved.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout.
      height - Target height, in pixels, for the image; required to provide vertical bounds for the layout.
    • Java2DRenderer

      public Java2DRenderer(File file, int width, int height) throws IOException
      Creates a new instance for a given File. Does not render until getImage() is called for the first time.
      Parameters:
      file - The file to be rendered.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout.
      height - Target height, in pixels, for the image
      Throws:
      IOException
    • Java2DRenderer

      public Java2DRenderer(Document doc, int width, int height)
      Creates a new instance pointing to the given Document. Does not render until getImage() is called for the first time.
      Parameters:
      doc - The document to be rendered.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout.
      height - Target height, in pixels, for the image.
    • Java2DRenderer

      public Java2DRenderer(Document doc, int width)
    • Java2DRenderer

      public Java2DRenderer(Document doc, String baseUrl, int width, int height)
      Creates a new instance pointing to the given Document. Does not render until getImage() is called for the first time.
      Parameters:
      doc - The document to be rendered.
      baseUrl - The base url for the document, against which relative paths are resolved.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout.
      height - Target height, in pixels, for the image.
    • Java2DRenderer

      public Java2DRenderer(File file, int width) throws IOException
      Creates a new instance for a given File. Does not render until getImage() is called for the first time.
      Parameters:
      file - The file to be rendered.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout. Height is calculated based on content
      Throws:
      IOException
    • Java2DRenderer

      public Java2DRenderer(String url, int width)
      Renderer for a given URL (which is also used as the base) and a specified width; height is calculated automatically.
      Parameters:
      url - The location of the document to be rendered.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout. Height is calculated based on content
    • Java2DRenderer

      public Java2DRenderer(String url, String baseurl, int width)
      Renderer for a given URL and a specified width; height is calculated automatically.
      Parameters:
      url - The location of the document to be rendered.
      baseurl - The base url for the document, against which relative paths are resolved.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout. Height is calculated based on content
    • Java2DRenderer

      public Java2DRenderer(String url, int width, int height)
      Renderer for a given URL and a specified width; height is calculated automatically.
      Parameters:
      url - The location of the document to be rendered.
      width - Target width, in pixels, for the image; required to provide horizontal bounds for the layout.
      height - Target height, in pixels, for the image
    • Java2DRenderer

      private Java2DRenderer(int width, int height, int bufferedImageType)
  • Method Details

    • fromFile

      public static Java2DRenderer fromFile(File file, int width, int bufferedImageType) throws IOException
      Throws:
      IOException
    • getSharedContext

      public SharedContext getSharedContext()
      Returns the SharedContext to be used by renderer. Is instantiated along with the class, so can be accessed before getImage() is called to tune the rendering process.
      Returns:
      the SharedContext instance that will be used by this renderer
    • getImage

      public BufferedImage getImage()
      Renders the XML document if necessary and returns the resulting image. If already rendered, same image reference will be returned.
      Returns:
      The XML rendered as a BufferedImage.
    • createBufferedImage

      protected BufferedImage createBufferedImage(int width, int height)
      Returns a BufferedImage using the specified width and height. By default, this returns an image compatible with the screen (if not in "headless" mode) using the BufferedImage type specified in constructor, or else RGB if none is specified.
      Parameters:
      width - target width
      height - target height
      Returns:
      new BI
    • setDocument

      private void setDocument(Document doc, String url, NamespaceHandler nsh)
    • layout

      private void layout(int width)
    • loadDocument

      private Document loadDocument(String uri)
    • newLayoutContext

      private LayoutContext newLayoutContext()
    • htmlAsImage

      public static BufferedImage htmlAsImage(String html, int widthInPixels) throws SAXException
      Throws:
      SAXException
    • htmlAsImage

      public static BufferedImage htmlAsImage(InputStream source, int widthInPixels) throws SAXException, IOException, ParserConfigurationException
      Throws:
      SAXException
      IOException
      ParserConfigurationException