Skip to content

Commit

Permalink
[error-prone] cleanup class Graphics2DRenderer
Browse files Browse the repository at this point in the history
* remove unused field `dim`
* replace `setDocument` methods by constructor with document parameter.
  • Loading branch information
asolntsev committed Oct 16, 2024
1 parent 01b6db3 commit 4d14841
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* for rendering documents directly to images.</p>
* <p>
* <p>Graphics2DRenderer supports the {@link XHTMLPanel#setDocument(Document)},
* {@link XHTMLPanel#doLayout()}, and {@link XHTMLPanel#render()} methods from
* {@link XHTMLPanel#doLayout()} method from
* {@link XHTMLPanel}, as well as easy-to-use static utility methods.
* For example, to render a document in an image that is 600 pixels wide use the
* {@link #renderToImageAutoSize(String,int,int)} method like this:</p>
Expand All @@ -46,25 +46,22 @@
*
* @author Joshua Marinacci
*/

public class Graphics2DRenderer {
public final class Graphics2DRenderer {
/**
* The panel we are using to render the document.
*/
private final XHTMLPanel panel;

/**
* Dimensions of the image to render, in pixels.
*/
@Nullable
protected Dimension dim;
public Graphics2DRenderer(String url) {
panel = new XHTMLPanel();
panel.setInteractive(false);
panel.setDocument(url);
}

/**
* Creates a new renderer with no document specified.
*/
public Graphics2DRenderer() {
public Graphics2DRenderer(Document doc, String base_url) {
panel = new XHTMLPanel();
panel.setInteractive(false);
panel.setDocument(doc, base_url);
}

// ASK maybe we could change the graphics2d to be a font rendering context?
Expand All @@ -76,7 +73,6 @@ public Graphics2DRenderer() {
* @param dim dimensions of the container for the document
*/
public void layout(Graphics2D g2, @Nullable Dimension dim) {
this.dim = dim;
if (dim != null) {
panel.setSize(dim);
}
Expand All @@ -96,27 +92,6 @@ public void render(Graphics2D g2) {
panel.paintComponent(g2);
}


/**
* Set the document to be rendered, lays it out, and
* renders it.
*
* @param url the URL for the document to render.
*/
public void setDocument(String url) {
panel.setDocument(url);
}

/**
* Sets the document to render, lays it out, and renders it.
*
* @param doc the Document to render
* @param base_url base URL for relative links within the Document.
*/
public void setDocument(Document doc, String base_url) {
panel.setDocument(doc, base_url);
}

/**
* Returns the size image needed to render the document without anything
* going off the side. Could be different from the dimensions passed into
Expand Down Expand Up @@ -174,8 +149,8 @@ public static BufferedImage renderToImage(String url, int width, int height) {
* @return Returns an Image containing the rendered document.
*/
public static BufferedImage renderToImage(String url, int width, int height, int bufferedImageType) {
Graphics2DRenderer g2r = new Graphics2DRenderer();
g2r.setDocument(url);
Graphics2DRenderer g2r = new Graphics2DRenderer(url);

Dimension dim = new Dimension(width, height);
BufferedImage buff = new BufferedImage((int) dim.getWidth(), (int) dim.getHeight(), bufferedImageType);
withGraphics(buff, g -> {
Expand All @@ -188,8 +163,8 @@ public static BufferedImage renderToImage(String url, int width, int height, int
/**
* A static utility method to automatically create an image from a
* document, where height is determined based on document content.
* To estimate a size before rendering, use {@link #setDocument(String)}
* and then {@link #getMinimumSize()}. The rendered image supports transparency.
* To estimate a size before rendering, use {@link #getMinimumSize()}.
* The rendered image supports transparency.
*
* @param url java.net.URL for the document to render.
* @param width Width in pixels of the layout container
Expand All @@ -202,8 +177,7 @@ public static BufferedImage renderToImageAutoSize(String url, int width){
/**
* A static utility method to automatically create an image from a
* document, where height is determined based on document content.
* To estimate a size before rendering, use {@link #setDocument(String)}
* and then {@link #getMinimumSize()}.
* To estimate a size before rendering, use {@link #getMinimumSize()}.
*
* @param url java.net.URL for the document to render.
* @param width Width in pixels of the layout container
Expand All @@ -212,8 +186,7 @@ public static BufferedImage renderToImageAutoSize(String url, int width){
* @return Returns java.awt.Image containing the rendered document.
*/
public static BufferedImage renderToImageAutoSize(String url, int width, int bufferedImageType) {
Graphics2DRenderer g2r = new Graphics2DRenderer();
g2r.setDocument(url);
Graphics2DRenderer g2r = new Graphics2DRenderer(url);
Dimension dim = new Dimension(width, 1000);

// do layout with temp buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public static void generateTestFile(String test, String diff, int width, int hei

public static String xhtmlToDiff(String xhtml, int width, int height) throws Exception {
Document doc = XMLUtil.documentFromFile(xhtml);
Graphics2DRenderer renderer = new Graphics2DRenderer();
renderer.setDocument(doc, new File(xhtml).toURI().toURL().toString());
Graphics2DRenderer renderer = new Graphics2DRenderer(doc, new File(xhtml).toURI().toURL().toString());

BufferedImage buff = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
withGraphics(buff, g -> {
Expand Down

0 comments on commit 4d14841

Please sign in to comment.