Skip to content

Commit

Permalink
For #73 - Method to output a single page and runners for the builder.…
Browse files Browse the repository at this point in the history
… [ci skip]
  • Loading branch information
danfickle committed Mar 6, 2017
1 parent 199a160 commit 1bde20f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class Java2DRenderer implements IJava2DRenderer {

private static final int DEFAULT_DOTS_PER_PIXEL = 1;
private static final int DEFAULT_DPI = 72;

private final int _initialPageNo;

/**
* Subject to change. Not public API. Used exclusively by the Java2DRendererBuilder class.
Expand All @@ -79,10 +81,11 @@ public Java2DRenderer(
String replacementText,
boolean testMode,
FSPageProcessor pageProcessor,
Graphics2D layoutGraphics) {
Graphics2D layoutGraphics,
int initialPageNumber) {

_pageProcessor = pageProcessor;

_initialPageNo = initialPageNumber;
_svgImpl = svgImpl;
_outputDevice = new Java2DOutputDevice(layoutGraphics);

Expand Down Expand Up @@ -276,11 +279,11 @@ private LayoutContext newLayoutContext() {
return result;
}

public void writePages(int initialPageNo) throws IOException {
public void writePages() throws IOException {
List<PageBox> pages = _root.getLayer().getPages();

RenderingContext c = newRenderingContext();
c.setInitialPageNo(initialPageNo);
c.setInitialPageNo(_initialPageNo);

PageBox firstPage = pages.get(0);
Rectangle2D firstPageSize = new Rectangle2D.Float(0, 0,
Expand All @@ -290,6 +293,41 @@ public void writePages(int initialPageNo) throws IOException {
writePageImages(pages, c, firstPageSize);
}

public void writePage(int zeroBasedPageNumber) throws IOException {
List<PageBox> pages = _root.getLayer().getPages();

if (zeroBasedPageNumber >= pages.size()) {
throw new IndexOutOfBoundsException();
}

RenderingContext c = newRenderingContext();
c.setInitialPageNo(_initialPageNo);

PageBox page = pages.get(zeroBasedPageNumber);

Rectangle2D pageSize = new Rectangle2D.Float(0, 0,
page.getWidth(c) / DEFAULT_DOTS_PER_PIXEL,
page.getHeight(c) / DEFAULT_DOTS_PER_PIXEL);

_outputDevice.setRoot(_root);

FSPage pg = _pageProcessor.createPage(zeroBasedPageNumber, (int) pageSize.getWidth(), (int) pageSize.getHeight());

_outputDevice.initializePage(pg.getGraphics());
_root.getLayer().assignPagePaintingPositions(c, Layer.PAGED_MODE_PRINT);

c.setPageCount(pages.size());
c.setPage(zeroBasedPageNumber, page);
paintPage(c, page);
_pageProcessor.finishPage(pg);

_outputDevice.finish(c, _root);
}

public int getPageCount() {
return _root.getLayer().getPages().size();
}

private void writePageImages(List<PageBox> pages, RenderingContext c, Rectangle2D firstPageSize) throws IOException {
_outputDevice.setRoot(_root);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -47,6 +48,7 @@ public class Java2DRendererBuilder {
private File _file;
private boolean _testMode = false;
private Graphics2D _layoutGraphics;
private int _initialPageNumber;

public static enum TextDirection { RTL, LTR; }
public static enum PageSizeUnits { MM, INCHES }
Expand Down Expand Up @@ -327,6 +329,16 @@ public Java2DRendererBuilder useDefaultPageSize(float pageWidth, float pageHeigh
this._isPageSizeInches = (units == PageSizeUnits.INCHES);
return this;
}

/**
* Used to set an initial page number for use with page counters, etc.
* @param pageNumberInitial
* @return
*/
public Java2DRendererBuilder useInitialPageNumber(int pageNumberInitial) {
this._initialPageNumber = pageNumberInitial;
return this;
}

/**
* Output the document in paged format. The user can use the DefaultPageProcessor or use its source
Expand All @@ -339,6 +351,32 @@ public Java2DRendererBuilder toPageProcessor(FSPageProcessor pageProcessor) {
return this;
}

/**
* <code>useLayoutGraphics</code> and <code>toPageProcessor</code> MUST have been called.
* Also a document MUST have been set with one of the with* methods.
* This will build the renderer and output each page of the document to the specified page
* processor.
* @throws Exception
*/
public void runPaged() throws Exception {
Java2DRenderer renderer = this.buildJava2DRenderer();
renderer.layout();
renderer.writePages();
}

/**
* <code>useLayoutGraphics</code> and <code>toPageProcessor</code> MUST have been called.
* Also a document MUST have been set with one of the with* methods.
* This will build the renderer and output the first page of the document to the specified page
* processor.
* @throws Exception
*/
public void runFirstPage() throws Exception {
Java2DRenderer renderer = this.buildJava2DRenderer();
renderer.layout();
renderer.writePage(0);
}

public Java2DRenderer buildJava2DRenderer() {
UnicodeImplementation unicode = new UnicodeImplementation(_reorderer, _splitter, _lineBreaker,
_unicodeToLowerTransformer, _unicodeToUpperTransformer, _unicodeToTitleTransformer, _textDirection, _charBreaker);
Expand All @@ -347,7 +385,7 @@ public Java2DRenderer buildJava2DRenderer() {

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

return new Java2DRenderer(doc, unicode, _httpStreamFactory, _resolver, _cache, _svgImpl, pageSize, _replacementText, _testMode, _pageProcessor, _layoutGraphics);
return new Java2DRenderer(doc, unicode, _httpStreamFactory, _resolver, _cache, _svgImpl, pageSize, _replacementText, _testMode, _pageProcessor, _layoutGraphics, _initialPageNumber);
}

public static abstract class Graphics2DPaintingReplacedElement extends EmptyReplacedElement {
Expand Down

0 comments on commit 1bde20f

Please sign in to comment.