Skip to content

Commit

Permalink
reformat XHTMLPrintable
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Nov 26, 2024
1 parent af48be6 commit 7e9a6c1
Showing 1 changed file with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
package org.xhtmlrenderer.simple;

import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.util.Uu;

import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

/**
* <p>XHTMLPrintable allows you to print XHTML content to a printer instead of
* rendering it to screen. It is an implementation of @see java.awt.print.Printable
* so you can use it any where you would use any other Printable object. The constructor
* requires an XHTMLPanel, so it's easiest to prepare an XHTMLPanel as normal, and then
* wrap a printable around it.ex:
* <p>
* XHTMLPrintable allows you to print XHTML content to a printer instead of
* rendering it to screen.
* </p>
*
* <p>
* It is an implementation of {@link java.awt.print.Printable},
* so you can use it whenever you would use any other Printable object. The constructor
* requires an {@link XHTMLPanel}, so it's easiest to prepare an {@link XHTMLPanel} instance as normal, and then
* wrap a printable around it.
* </p>
*
* <p>
* <pre>@{code
* import org.xhtmlrenderer.simple.*;
* import java.awt.print.*;
* // . . . .
* // xhtml_panel created earlier
* For example:
* <pre>{@code
* import org.xhtmlrenderer.simple.*;
* import java.awt.print.*;
* // . . . .
* // xhtml_panel created earlier
*
* PrinterJob printJob = PrinterJob.getPrinterJob();
* printJob.setPrintable(new XHTMLPrintable(xhtml_panel));
* PrinterJob printJob = PrinterJob.getPrinterJob();
* printJob.setPrintable(new XHTMLPrintable(xhtml_panel));
*
* if(printJob.printDialog()) {
* printJob.print();
* }
* if (printJob.printDialog()) {
* printJob.print();
* }
* }</pre>
*/

public class XHTMLPrintable implements Printable {

private final XHTMLPanel panel;

@Nullable
private Graphics2DRenderer g2r;


Expand All @@ -50,19 +59,18 @@ public XHTMLPrintable(XHTMLPanel panel) {
* <p>The implementation of the <i>print</i> method
* from the @see java.awt.print.Printable interface.
*/
@Override
public int print(Graphics g, PageFormat pf, int page) {
try {

Graphics2D g2 = (Graphics2D) g;

if (g2r == null) {
g2r = new Graphics2DRenderer();
g2r = new Graphics2DRenderer(panel.getDocument(), panel.getSharedContext().getUac().getBaseURL());
g2r.getSharedContext().setPrint(true);
g2r.getSharedContext().setInteractive(false);
g2r.getSharedContext().setDPI(72f);
g2r.getSharedContext().getTextRenderer().setSmoothingThreshold(0);
g2r.getSharedContext().setUserAgentCallback(panel.getSharedContext().getUserAgentCallback());
g2r.setDocument(panel.getDocument(), panel.getSharedContext().getUac().getBaseURL());
g2r.getSharedContext().setReplacedElementFactory(panel.getSharedContext().getReplacedElementFactory());
g2r.layout(g2, null);
g2r.getPanel().assignPagePrintPositions(g2);
Expand Down

0 comments on commit 7e9a6c1

Please sign in to comment.