Skip to content

Commit

Permalink
propagate the diagnostic consumer inside the *Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jul 10, 2020
1 parent 109ad5a commit e705452
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ public class Java2DRenderer implements Closeable {
private final int _initialPageNo;
private final short _pagingMode;

private final Closeable diagnosticConsumer;


/**
* Subject to change. Not public API. Used exclusively by the Java2DRendererBuilder class.
*/
public Java2DRenderer(BaseDocument doc, UnicodeImplementation unicode, PageDimensions pageSize,
Java2DRendererBuilderState state) {
Java2DRendererBuilderState state, Closeable diagnosticConsumer) {

this.diagnosticConsumer = diagnosticConsumer;
_pagingMode = state._pagingMode;
_pageProcessor = state._pageProcessor;
_initialPageNo = state._initialPageNumber;
Expand Down Expand Up @@ -440,6 +443,12 @@ public void close() {
_sharedContext.removeFromThread();
ThreadCtx.cleanup();

try {
diagnosticConsumer.close();
} catch (IOException e) {

}

if (_svgImpl != null) {
try {
_svgImpl.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public Java2DRendererBuilder toPageProcessor(FSPageProcessor pageProcessor) {
* @throws Exception
*/
public void runPaged() throws Exception {
try (Closeable d = this.applyDiagnosticConsumer()) {
Java2DRenderer renderer = this.buildJava2DRenderer();
try (Closeable d = this.applyDiagnosticConsumer(); Java2DRenderer renderer = this.buildJava2DRenderer(d)) {
renderer.layout();
if (state._pagingMode == Layer.PAGED_MODE_PRINT)
renderer.writePages();
Expand All @@ -108,8 +107,7 @@ public void runPaged() throws Exception {
* @throws Exception
*/
public void runFirstPage() throws Exception {
try (Closeable d = this.applyDiagnosticConsumer()) {
Java2DRenderer renderer = this.buildJava2DRenderer();
try (Closeable d = this.applyDiagnosticConsumer(); Java2DRenderer renderer = this.buildJava2DRenderer(d)) {
renderer.layout();
if (state._pagingMode == Layer.PAGED_MODE_PRINT)
renderer.writePage(0);
Expand All @@ -119,6 +117,10 @@ public void runFirstPage() throws Exception {
}

public Java2DRenderer buildJava2DRenderer() {
return buildJava2DRenderer(this.applyDiagnosticConsumer());
}

public Java2DRenderer buildJava2DRenderer(Closeable diagnosticConsumer) {

UnicodeImplementation unicode = new UnicodeImplementation(state._reorderer, state._splitter, state._lineBreaker,
state._unicodeToLowerTransformer, state._unicodeToUpperTransformer, state._unicodeToTitleTransformer, state._textDirection,
Expand All @@ -136,7 +138,7 @@ public Java2DRenderer buildJava2DRenderer() {
state._layoutGraphics = bf.createGraphics();
}

return new Java2DRenderer(doc, unicode, pageSize, state);
return new Java2DRenderer(doc, unicode, pageSize, state, diagnosticConsumer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ public class PdfBoxRenderer implements Closeable, PageSupplier {
private final boolean _useFastMode;

private PageSupplier _pageSupplier;

private final Closeable diagnosticConsumer;

/**
* This method is constantly changing as options are added to the builder.
*/
PdfBoxRenderer(BaseDocument doc, UnicodeImplementation unicode,
PageDimensions pageSize, PdfRendererBuilderState state) {
PageDimensions pageSize, PdfRendererBuilderState state, Closeable diagnosticConsumer) {

this.diagnosticConsumer = diagnosticConsumer;

_pdfDoc = state.pddocument != null ? state.pddocument : new PDDocument();
_pdfDoc.setVersion(state._pdfVersion);
Expand Down Expand Up @@ -1052,6 +1056,10 @@ public void setListener(PDFCreationListener listener) {
public void cleanup() {
_outputDevice.close();
_sharedContext.removeFromThread();
try {
diagnosticConsumer.close();
} catch (IOException e) {
}
ThreadCtx.cleanup();

// Close all still open font files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ public PdfRendererBuilder() {
* @throws IOException
*/
public void run() throws IOException {
PdfBoxRenderer renderer = null;
try (Closeable d = applyDiagnosticConsumer()){
renderer = this.buildPdfRenderer();
try (Closeable d = applyDiagnosticConsumer(); PdfBoxRenderer renderer = this.buildPdfRenderer(d)){
renderer.layout();
renderer.createPDF();
} finally {
if (renderer != null) {
renderer.close();
}
}
}

Expand All @@ -56,6 +50,10 @@ public void run() throws IOException {
* @return
*/
public PdfBoxRenderer buildPdfRenderer() {
return buildPdfRenderer(applyDiagnosticConsumer());
}

public PdfBoxRenderer buildPdfRenderer(Closeable diagnosticConsumer) {
UnicodeImplementation unicode = new UnicodeImplementation(state._reorderer, state._splitter, state._lineBreaker,
state._unicodeToLowerTransformer, state._unicodeToUpperTransformer, state._unicodeToTitleTransformer, state._textDirection,
state._charBreaker);
Expand All @@ -64,7 +62,7 @@ public PdfBoxRenderer buildPdfRenderer() {

BaseDocument doc = new BaseDocument(state._baseUri, state._html, state._document, state._file, state._uri);

PdfBoxRenderer renderer = new PdfBoxRenderer(doc, unicode, pageSize, state);
PdfBoxRenderer renderer = new PdfBoxRenderer(doc, unicode, pageSize, state, diagnosticConsumer);

/*
* Register all Fonts
Expand Down

0 comments on commit e705452

Please sign in to comment.