Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SideMargin and SpacingTop and SpacingBottom for the PdfView #540

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ pdfView.fromAsset(String)
.enableAntialiasing(true) // improve rendering a little bit on low-res screens
// spacing between pages in dp. To define spacing color, set view background
.spacing(0)
// spacing on the left and right if the pageFitPolicy is WIDTH in dp. To define spacing color, set view background
.sideMargin(0)
// spacing above the first page in dp. To define spacing color, set view background
.spacingTop(0)
// spacing below the last page in dp. To define spacing color, set view background
.spacingBottom(0)
.linkHandler(DefaultLinkHandler)
.pageFitPolicy(FitPolicy.WIDTH)
.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected Throwable doInBackground(Void... params) {
try {
PdfDocument pdfDocument = docSource.createDocument(pdfView.getContext(), pdfiumCore, password);
pdfFile = new PdfFile(pdfiumCore, pdfDocument, pdfView.getPageFitPolicy(), getViewSize(),
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx());
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.getSpacingTopPx(), pdfView.getSpacingBottomPx(), pdfView.getSideMarginPx());
return null;
} catch (Throwable t) {
return t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ ScrollHandle getScrollHandle() {
/** Spacing between pages, in px */
private int spacingPx = 0;

/** Spacing above the first page, in px */
private int spacingTopPx = 0;

/** Spacing below the last page, in px */
private int spacingBottomPx = 0;

/** Spacing Left and Right in px */
private int sideMarginPx = 0;

/** used to render the top margin only the first render */
private boolean initialRender = true;

/** Pages numbers used when calling onDrawAllListener */
private List<Integer> onDrawPagesNums = new ArrayList<>(10);

Expand Down Expand Up @@ -274,7 +286,11 @@ public void jumpTo(int page, boolean withAnimation) {
}

page = pdfFile.determineValidPageNumberFrom(page);
float offset = -pdfFile.getPageOffset(page, zoom);
float offset = -pdfFile.getPageOffset(page, zoom);;
if (page == 0 && initialRender) {
initialRender = false;
offset += spacingTopPx;
}
if (swipeVertical) {
if (withAnimation) {
animationManager.startYAnimation(currentYOffset, offset);
Expand Down Expand Up @@ -616,8 +632,8 @@ private void drawPart(Canvas canvas, PagePart part) {
}

// Move to the target page
float localTranslationX = 0;
float localTranslationY = 0;
float localTranslationX;
float localTranslationY;
SizeF size = pdfFile.getPageSize(part.getPage());

if (swipeVertical) {
Expand Down Expand Up @@ -664,7 +680,6 @@ private void drawPart(Canvas canvas, PagePart part) {

// Restore the canvas position
canvas.translate(-localTranslationX, -localTranslationY);

}

/**
Expand Down Expand Up @@ -1069,6 +1084,30 @@ private void setSpacing(int spacing) {
this.spacingPx = Util.getDP(getContext(), spacing);
}

int getSpacingTopPx() {
return spacingTopPx;
}

private void setSpacingTop(int spacingBottom) {
this.spacingTopPx = Util.getDP(getContext(), spacingBottom);
}

int getSpacingBottomPx() {
return spacingBottomPx;
}

private void setSpacingBottom(int spacingBottom) {
this.spacingBottomPx = Util.getDP(getContext(), spacingBottom);
}

int getSideMarginPx() {
return sideMarginPx;
}

private void setSideMargin(int sideMargin) {
this.sideMarginPx = Util.getDP(getContext(), sideMargin);
}

private void setPageFitPolicy(FitPolicy pageFitPolicy) {
this.pageFitPolicy = pageFitPolicy;
}
Expand Down Expand Up @@ -1181,6 +1220,12 @@ public class Configurator {

private int spacing = 0;

private int spacingTop = 0;

private int spacingBottom = 0;

private int sideMargin = 0;

private FitPolicy pageFitPolicy = FitPolicy.WIDTH;

private Configurator(DocumentSource documentSource) {
Expand Down Expand Up @@ -1287,6 +1332,21 @@ public Configurator spacing(int spacing) {
return this;
}

public Configurator spacingTop(int spacingTop) {
this.spacingTop = spacingTop;
return this;
}

public Configurator spacingBottom(int spacingBottom) {
this.spacingBottom = spacingBottom;
return this;
}

public Configurator sideMargin(int sideMargin) {
this.sideMargin = sideMargin;
return this;
}

public Configurator pageFitPolicy(FitPolicy pageFitPolicy) {
this.pageFitPolicy = pageFitPolicy;
return this;
Expand Down Expand Up @@ -1316,6 +1376,9 @@ public void load() {
PDFView.this.setScrollHandle(scrollHandle);
PDFView.this.enableAntialiasing(antialiasing);
PDFView.this.setSpacing(spacing);
PDFView.this.setSpacingTop(spacingTop);
PDFView.this.setSpacingBottom(spacingBottom);
PDFView.this.setSideMargin(sideMargin);
PDFView.this.setPageFitPolicy(pageFitPolicy);

if (pageNumbers != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class PdfFile {
private SizeF maxWidthPageSize = new SizeF(0, 0);
private boolean isVertical = true;
private int spacingPx = 0;
private int spacingTopPx = 0;
private int spacingBottomPx = 0;
private int sideMargin = 0;
/** Calculated offsets for pages */
private List<Float> pageOffsets = new ArrayList<>();
/** Calculated document length (width or height, depending on swipe mode) */
Expand All @@ -65,13 +68,16 @@ class PdfFile {
private int[] originalUserPages;

PdfFile(PdfiumCore pdfiumCore, PdfDocument pdfDocument, FitPolicy pageFitPolicy, Size viewSize, int[] originalUserPages,
boolean isVertical, int spacing) {
boolean isVertical, int spacing, int spacingTop, int spacingBottom, int sideMargin) {
this.pdfiumCore = pdfiumCore;
this.pdfDocument = pdfDocument;
this.pageFitPolicy = pageFitPolicy;
this.originalUserPages = originalUserPages;
this.isVertical = isVertical;
this.spacingPx = spacing;
this.spacingTopPx = spacingTop;
this.spacingBottomPx = spacingBottom;
this.sideMargin = sideMargin;
setup(viewSize);
}

Expand Down Expand Up @@ -104,7 +110,7 @@ private void setup(Size viewSize) {
public void recalculatePageSizes(Size viewSize) {
pageSizes.clear();
PageSizeCalculator calculator = new PageSizeCalculator(pageFitPolicy, originalMaxWidthPageSize,
originalMaxHeightPageSize, viewSize);
originalMaxHeightPageSize, viewSize, sideMargin);
maxWidthPageSize = calculator.getOptimalMaxWidthPageSize();
maxHeightPageSize = calculator.getOptimalMaxHeightPageSize();

Expand Down Expand Up @@ -155,18 +161,18 @@ private void prepareDocLen() {
for (SizeF pageSize : pageSizes) {
length += isVertical ? pageSize.getHeight() : pageSize.getWidth();
}
int spacing = spacingPx * (pageSizes.size() - 1);
int spacing = (spacingPx * (pageSizes.size() - 1)) + spacingTopPx + spacingBottomPx;
documentLength = length + spacing;
}

private void preparePagesOffset() {
pageOffsets.clear();
float offset = 0;
float offset = spacingTopPx;
for (int i = 0; i < getPagesCount(); i++) {
float spacing = i * spacingPx;
pageOffsets.add(offset + spacing);
SizeF size = pageSizes.get(i);
offset += isVertical ? size.getHeight() : size.getWidth();
offset += isVertical ? size.getHeight() : size.getWidth();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public class PageSizeCalculator {
private SizeF optimalMaxHeightPageSize;
private float widthRatio;
private float heightRatio;
private int sideMargin;

public PageSizeCalculator(FitPolicy fitPolicy, Size originalMaxWidthPageSize, Size originalMaxHeightPageSize,
Size viewSize) {
Size viewSize, int sideMargin) {
this.fitPolicy = fitPolicy;
this.originalMaxWidthPageSize = originalMaxWidthPageSize;
this.originalMaxHeightPageSize = originalMaxHeightPageSize;
this.viewSize = viewSize;
this.sideMargin = sideMargin;
calculateMaxPages();
}

Expand All @@ -48,7 +50,7 @@ public SizeF calculate(Size pageSize) {
case BOTH:
return fitBoth(pageSize, pageSize.getWidth() * widthRatio, pageSize.getHeight() * heightRatio);
default:
return fitWidth(pageSize, pageSize.getWidth() * widthRatio);
return fitWidth(pageSize, (pageSize.getWidth() - (sideMargin * 2)) * widthRatio);
}
}

Expand Down Expand Up @@ -85,7 +87,8 @@ private void calculateMaxPages() {
}

private SizeF fitWidth(Size pageSize, float maxWidth) {
float w = pageSize.getWidth(), h = pageSize.getHeight();
float w = pageSize.getWidth();
float h = pageSize.getHeight();
float ratio = w / h;
w = maxWidth;
h = (float) Math.floor(maxWidth / ratio);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ private void displayFromAsset(String assetFileName) {
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.spacing(10) // in dp
.spacingTop(500) // in dp
.spacingBottom(500) // in dp
.sideMargin(50) // in dp (only supported with FitPolicy.WIDTH)
.onPageError(this)
.pageFitPolicy(FitPolicy.BOTH)
.pageFitPolicy(FitPolicy.HEIGHT)
.load();
}

Expand All @@ -136,7 +139,11 @@ private void displayFromUri(Uri uri) {
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.spacing(10) // in dp
.spacingTop(500) // in dp
.spacingBottom(500) // in dp
.sideMargin(50) // in dp (only supported with FitPolicy.WIDTH)
.onPageError(this)
.pageFitPolicy(FitPolicy.WIDTH)
.load();
}

Expand Down