Package org.xhtmlrenderer.swing
Class BoxRenderer
java.lang.Object
org.xhtmlrenderer.swing.BoxRenderer
Renders an XML files, formatted with CSS, as an Box. Input is a document in the form of file or URL, and output is the root of the Box model. A BoxRenderer is not intended to be re-used for multiple document sources; just create new BoxRenderers for each one you need. BoxRenderer is not thread-safe. Standard usage pattern is
File xhtml = ... BoxRenderer rend = new BoxRenderer(xhtml); Box rootBox = rend.render();
The document is not loaded, and layout and render don't take place, until render()
is called.
Subsequent calls to render()
don't result in a reload; create a new BoxRenderer 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()
.
Not thread-safe.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final int
private static final int
private Document
private int
private LayoutContext
private static final int
private Java2DOutputDevice
private boolean
Whether we've completed rendering; image will only be rendered once.private Box
private SharedContext
private final String
private final String
private final int
-
Constructor Summary
ConstructorsConstructorDescriptionBoxRenderer
(File file, int width) Creates a new instance for a given File.BoxRenderer
(File file, int width, int height) Creates a new instance for a given File.BoxRenderer
(String url, int width) Renderer for a given URL (which is also used as the base) and a specified width; height is calculated automatically.BoxRenderer
(String url, int width, int height) Renderer for a given URL and a specified width; height is calculated automatically.BoxRenderer
(String url, String baseurl, int width) Renderer for a given URL and a specified width; height is calculated automatically.BoxRenderer
(String url, String baseUrl, int width, int height) Creates a new instance for a given URL. -
Method Summary
Modifier and TypeMethodDescriptionprotected BufferedImage
createBufferedImage
(int width, int height) Returns a BufferedImage using the specified width and height.Returns the LayoutContext to be used by renderer.Returns the SharedContext to be used by renderer.private void
init()
private void
layout
(int width) private Document
loadDocument
(String uri) private LayoutContext
private SharedContext
newSharedContext
(UserAgentCallback userAgent) render()
Renders the XML document if necessary and returns the root Box.private void
setDocument
(Document doc, String url, NamespaceHandler nsh) void
setRenderingHints
(Map<Object, Object> hints) Sets the rendering hints to apply to the Graphics2D instance used by the renderer; seeGraphics2D.setRenderingHints(java.util.Map)
.
-
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:
-
outputDevice
-
doc
-
root
-
rendered
private boolean renderedWhether we've completed rendering; image will only be rendered once. -
sourceDocument
-
sourceDocumentBase
-
width
private final int width -
height
private int height -
NO_HEIGHT
private static final int NO_HEIGHT- See Also:
-
renderingHints
-
layoutContext
-
-
Constructor Details
-
BoxRenderer
Creates a new instance for a given URL. Does not render untilrender()
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.
-
BoxRenderer
Creates a new instance for a given File. Does not render untilrender()
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
-
BoxRenderer
Creates a new instance for a given File. Does not render untilrender()
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
-
BoxRenderer
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
-
BoxRenderer
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
-
BoxRenderer
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
-
-
Method Details
-
setRenderingHints
Sets the rendering hints to apply to the Graphics2D instance used by the renderer; seeGraphics2D.setRenderingHints(java.util.Map)
. The Map need not specify values for all properties; any settings in this map will be applied as override to the default settings, and will not replace the entire Map for the Graphics2D instance.- Parameters:
hints
- values to override in default rendering hints for Graphics2D we are rendering to
-
getLayoutContext
Returns the LayoutContext to be used by renderer. Is instantiated along with the class, so can be accessed beforerender()
is called to tune the rendering process.- Returns:
- the LayoutContext instance that will be used by this renderer
-
render
Renders the XML document if necessary and returns the root Box. If already rendered, same Box reference will be returned.- Returns:
- The XML rendered as a Box.
-
createBufferedImage
Returns a BufferedImage using the specified width and height. By default, this returns an image compatible with the screen (if not in "headless" mode) formatted for RGB.- Parameters:
width
- target widthheight
- target height- Returns:
- new BI
-
setDocument
-
layout
private void layout(int width) -
loadDocument
-
newLayoutContext
-
init
private void init()
-