Skip to content

Commit

Permalink
enhancement: support for landscape pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ogmios-voice committed Sep 16, 2019
1 parent 8a2970a commit 170a375
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
21 changes: 14 additions & 7 deletions src/main/java/be/quodlibet/boxable/BaseTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@
*/
public class BaseTable extends Table<PDPage> {

public BaseTable(float yStart, float yStartNewPage, float bottomMargin, float width, float margin, PDDocument document, PDPage currentPage, boolean drawLines, boolean drawContent) throws IOException {
super(yStart, yStartNewPage, 0, bottomMargin, width, margin, document, currentPage, drawLines, drawContent, new DefaultPageProvider(document, currentPage.getMediaBox()));
public BaseTable(float yStart, float yStartNewPage, float bottomMargin, float width, float margin,
PDDocument document, PDPage currentPage, boolean drawLines, boolean drawContent) throws IOException {
this(yStart, yStartNewPage, 0, bottomMargin, width, margin, document, currentPage, drawLines, drawContent, newPageProvider(document, currentPage));
}

public BaseTable(float yStart, float yStartNewPage, float pageTopMargin, float bottomMargin, float width, float margin, PDDocument document, PDPage currentPage, boolean drawLines, boolean drawContent) throws IOException {
super(yStart, yStartNewPage, pageTopMargin, bottomMargin, width, margin, document, currentPage, drawLines, drawContent, new DefaultPageProvider(document, currentPage.getMediaBox()));

public BaseTable(float yStart, float yStartNewPage, float pageTopMargin, float bottomMargin, float width, float margin,
PDDocument document, PDPage currentPage, boolean drawLines, boolean drawContent) throws IOException {
this(yStart, yStartNewPage, pageTopMargin, bottomMargin, width, margin, document, currentPage, drawLines, drawContent, newPageProvider(document, currentPage));
}

public BaseTable(float yStart, float yStartNewPage, float pageTopMargin, float bottomMargin, float width, float margin, PDDocument document, PDPage currentPage, boolean drawLines, boolean drawContent, final PageProvider<PDPage> pageProvider) throws IOException {

public BaseTable(float yStart, float yStartNewPage, float pageTopMargin, float bottomMargin, float width, float margin,
PDDocument document, PDPage currentPage, boolean drawLines, boolean drawContent, PageProvider<PDPage> pageProvider) throws IOException {
super(yStart, yStartNewPage, pageTopMargin, bottomMargin, width, margin, document, currentPage, drawLines, drawContent, pageProvider);
}

protected static DefaultPageProvider newPageProvider(final PDDocument doc, final PDPage page) {
return new DefaultPageProvider(doc, page.getMediaBox(), page.getRotation());
}

@Override
protected void loadFonts() {
// Do nothing as we don't have any fonts to load
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/be/quodlibet/boxable/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
Expand Down Expand Up @@ -344,7 +345,7 @@ private void drawRow(Row<T> row) throws IOException {
*
* @return
*/
private T createNewPage() {
private T createNewPage() throws IOException {
if (pageProvider != null) {
return pageProvider.nextPage();
}
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/be/quodlibet/boxable/page/DefaultPageProvider.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package be.quodlibet.boxable.page;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.util.Matrix;

public class DefaultPageProvider implements PageProvider<PDPage> {

public static final int ANG_PORTRAIT = 0;
public static final int ANG_LANDSCAPE = 90;

private final PDDocument document;

private final PDRectangle size;
private final int rotation;

private int currentPageIndex = -1;

public DefaultPageProvider(final PDDocument document, final PDRectangle size) {
this(document, size, 0);
}

public DefaultPageProvider(final PDDocument document, final PDRectangle size, final int rotation) {
this.document = document;
this.size = size;
this.rotation = rotation;
}

@Override
Expand All @@ -23,13 +37,13 @@ public PDDocument getDocument() {
}

@Override
public PDPage createPage() {
public PDPage createPage() throws IOException {
currentPageIndex = document.getNumberOfPages();
return getCurrentPage();
}

@Override
public PDPage nextPage() {
public PDPage nextPage() throws IOException {
if (currentPageIndex == -1) {
currentPageIndex = document.getNumberOfPages();
} else {
Expand All @@ -40,7 +54,7 @@ public PDPage nextPage() {
}

@Override
public PDPage previousPage() {
public PDPage previousPage() throws IOException {
currentPageIndex--;
if (currentPageIndex < 0) {
currentPageIndex = 0;
Expand All @@ -49,14 +63,31 @@ public PDPage previousPage() {
return getCurrentPage();
}

private PDPage getCurrentPage() {
private PDPage getCurrentPage() throws IOException {
if (currentPageIndex >= document.getNumberOfPages()) {
final PDPage newPage = new PDPage(size);
newPage.setRotation(rotation);
if (rotation == ANG_LANDSCAPE) { // => change ref. for drawing
addContStrmRot(newPage);
}
document.addPage(newPage);
return newPage;
}

return document.getPage(currentPageIndex);
}

protected void addContStrmRot(final PDPage pg) throws IOException {
final PDPageContentStream cont = new PDPageContentStream(getDocument(), pg, AppendMode.APPEND, true);
cont.transform(newTransfMtxOrientL(pg));
cont.close();
}

protected Matrix newTransfMtxOrientL(final PDPage pg) {
return newTransfMtxOrientL(pg.getMediaBox().getWidth());
}

protected Matrix newTransfMtxOrientL(final float w) {
return new Matrix(0, 1, -1, 0, w, 0);
}
}
8 changes: 5 additions & 3 deletions src/main/java/be/quodlibet/boxable/page/PageProvider.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package be.quodlibet.boxable.page;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public interface PageProvider<T extends PDPage> {

T createPage();
T createPage() throws IOException;

T nextPage();
T nextPage() throws IOException;

T previousPage();
T previousPage() throws IOException;

PDDocument getDocument();
}

0 comments on commit 170a375

Please sign in to comment.