Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65b4282

Browse files
committed
Merge branch 'Flamedek-pagefit'
2 parents b9f8953 + 23dd781 commit 65b4282

File tree

6 files changed

+134
-104
lines changed

6 files changed

+134
-104
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ pdfView.fromAsset(String)
102102
.spacing(0)
103103
.autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
104104
.linkHandler(DefaultLinkHandler)
105-
.pageFitPolicy(FitPolicy.WIDTH)
106-
.pageSnap(true) // snap pages to screen boundaries
105+
.pageFitPolicy(FitPolicy.WIDTH) // mode to fit pages in the view
106+
.fitEachPage(false) // fit each page to the view, else smaller pages are scaled relative to largest page.
107+
.pageSnap(false) // snap pages to screen boundaries
107108
.pageFling(false) // make a fling change only a single page like ViewPager
108109
.nightMode(false) // toggle night mode
109110
.load();
Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,89 @@
1-
/**
2-
* Copyright 2016 Bartosz Schiller
3-
* <p/>
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
* <p/>
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
* <p/>
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
package com.github.barteksc.pdfviewer;
17-
18-
import android.os.AsyncTask;
19-
20-
import com.github.barteksc.pdfviewer.source.DocumentSource;
21-
import com.shockwave.pdfium.PdfDocument;
22-
import com.shockwave.pdfium.PdfiumCore;
23-
import com.shockwave.pdfium.util.Size;
24-
25-
import java.lang.ref.WeakReference;
26-
27-
class DecodingAsyncTask extends AsyncTask<Void, Void, Throwable> {
28-
29-
private boolean cancelled;
30-
31-
private WeakReference<PDFView> pdfViewReference;
32-
33-
private PdfiumCore pdfiumCore;
34-
private String password;
35-
private DocumentSource docSource;
36-
private int[] userPages;
37-
private PdfFile pdfFile;
38-
39-
DecodingAsyncTask(DocumentSource docSource, String password, int[] userPages, PDFView pdfView, PdfiumCore pdfiumCore) {
40-
this.docSource = docSource;
41-
this.userPages = userPages;
42-
this.cancelled = false;
43-
this.pdfViewReference = new WeakReference<>(pdfView);
44-
this.password = password;
45-
this.pdfiumCore = pdfiumCore;
46-
}
47-
48-
@Override
49-
protected Throwable doInBackground(Void... params) {
50-
try {
51-
PDFView pdfView = pdfViewReference.get();
52-
if (pdfView != null) {
53-
PdfDocument pdfDocument = docSource.createDocument(pdfView.getContext(), pdfiumCore, password);
54-
pdfFile = new PdfFile(pdfiumCore, pdfDocument, pdfView.getPageFitPolicy(), getViewSize(pdfView),
55-
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.doAutoSpacing());
56-
return null;
57-
} else {
58-
return new NullPointerException("pdfView == null");
59-
}
60-
61-
} catch (Throwable t) {
62-
return t;
63-
}
64-
}
65-
66-
private Size getViewSize(PDFView pdfView) {
67-
return new Size(pdfView.getWidth(), pdfView.getHeight());
68-
}
69-
70-
@Override
71-
protected void onPostExecute(Throwable t) {
72-
PDFView pdfView = pdfViewReference.get();
73-
if (pdfView != null) {
74-
if (t != null) {
75-
pdfView.loadError(t);
76-
return;
77-
}
78-
if (!cancelled) {
79-
pdfView.loadComplete(pdfFile);
80-
}
81-
}
82-
}
83-
84-
@Override
85-
protected void onCancelled() {
86-
cancelled = true;
87-
}
88-
}
1+
/**
2+
* Copyright 2016 Bartosz Schiller
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.barteksc.pdfviewer;
17+
18+
import android.os.AsyncTask;
19+
20+
import com.github.barteksc.pdfviewer.source.DocumentSource;
21+
import com.shockwave.pdfium.PdfDocument;
22+
import com.shockwave.pdfium.PdfiumCore;
23+
import com.shockwave.pdfium.util.Size;
24+
25+
import java.lang.ref.WeakReference;
26+
27+
class DecodingAsyncTask extends AsyncTask<Void, Void, Throwable> {
28+
29+
private boolean cancelled;
30+
31+
private WeakReference<PDFView> pdfViewReference;
32+
33+
private PdfiumCore pdfiumCore;
34+
private String password;
35+
private DocumentSource docSource;
36+
private int[] userPages;
37+
private PdfFile pdfFile;
38+
39+
DecodingAsyncTask(DocumentSource docSource, String password, int[] userPages, PDFView pdfView, PdfiumCore pdfiumCore) {
40+
this.docSource = docSource;
41+
this.userPages = userPages;
42+
this.cancelled = false;
43+
this.pdfViewReference = new WeakReference<>(pdfView);
44+
this.password = password;
45+
this.pdfiumCore = pdfiumCore;
46+
}
47+
48+
@Override
49+
protected Throwable doInBackground(Void... params) {
50+
try {
51+
PDFView pdfView = pdfViewReference.get();
52+
if (pdfView != null) {
53+
PdfDocument pdfDocument = docSource.createDocument(pdfView.getContext(), pdfiumCore, password);
54+
pdfFile = new PdfFile(pdfiumCore, pdfDocument, pdfView.getPageFitPolicy(), getViewSize(pdfView),
55+
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.isAutoSpacingEnabled(),
56+
pdfView.isFitEachPage());
57+
return null;
58+
} else {
59+
return new NullPointerException("pdfView == null");
60+
}
61+
62+
} catch (Throwable t) {
63+
return t;
64+
}
65+
}
66+
67+
private Size getViewSize(PDFView pdfView) {
68+
return new Size(pdfView.getWidth(), pdfView.getHeight());
69+
}
70+
71+
@Override
72+
protected void onPostExecute(Throwable t) {
73+
PDFView pdfView = pdfViewReference.get();
74+
if (pdfView != null) {
75+
if (t != null) {
76+
pdfView.loadError(t);
77+
return;
78+
}
79+
if (!cancelled) {
80+
pdfView.loadComplete(pdfFile);
81+
}
82+
}
83+
}
84+
85+
@Override
86+
protected void onCancelled() {
87+
cancelled = true;
88+
}
89+
}

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/DragPinchManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
204204
if (!pdfView.isSwipeEnabled()) {
205205
return false;
206206
}
207-
if (pdfView.doPageFling()) {
207+
if (pdfView.isPageFlingEnabled()) {
208208
if (pdfView.pageFillsScreen()) {
209209
onBoundedFling(velocityX, velocityY);
210210
} else {

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/PDFView.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ enum ScrollDir {
172172
/** Policy for fitting pages to screen */
173173
private FitPolicy pageFitPolicy = FitPolicy.WIDTH;
174174

175+
private boolean fitEachPage = false;
176+
175177
private int defaultPage = 0;
176178

177179
/** True if should scroll through pages vertically instead of horizontally */
@@ -1213,20 +1215,20 @@ public int getSpacingPx() {
12131215
return spacingPx;
12141216
}
12151217

1216-
public boolean doAutoSpacing() {
1218+
public boolean isAutoSpacingEnabled() {
12171219
return autoSpacing;
12181220
}
12191221

12201222
public void setPageFling(boolean pageFling) {
12211223
this.pageFling = pageFling;
12221224
}
12231225

1224-
public boolean doPageFling() {
1226+
public boolean isPageFlingEnabled() {
12251227
return pageFling;
12261228
}
12271229

1228-
private void setSpacing(int spacing) {
1229-
this.spacingPx = Util.getDP(getContext(), spacing);
1230+
private void setSpacing(int spacingDp) {
1231+
this.spacingPx = Util.getDP(getContext(), spacingDp);
12301232
}
12311233

12321234
private void setAutoSpacing(boolean autoSpacing) {
@@ -1241,7 +1243,15 @@ public FitPolicy getPageFitPolicy() {
12411243
return pageFitPolicy;
12421244
}
12431245

1244-
public boolean doPageSnap() {
1246+
private void setFitEachPage(boolean fitEachPage) {
1247+
this.fitEachPage = fitEachPage;
1248+
}
1249+
1250+
public boolean isFitEachPage() {
1251+
return fitEachPage;
1252+
}
1253+
1254+
public boolean isPageSnap() {
12451255
return pageSnap;
12461256
}
12471257

@@ -1359,6 +1369,8 @@ public class Configurator {
13591369

13601370
private FitPolicy pageFitPolicy = FitPolicy.WIDTH;
13611371

1372+
private boolean fitEachPage = false;
1373+
13621374
private boolean pageFling = false;
13631375

13641376
private boolean pageSnap = false;
@@ -1484,6 +1496,11 @@ public Configurator pageFitPolicy(FitPolicy pageFitPolicy) {
14841496
return this;
14851497
}
14861498

1499+
public Configurator fitEachPage(boolean fitEachPage) {
1500+
this.fitEachPage = fitEachPage;
1501+
return this;
1502+
}
1503+
14871504
public Configurator pageSnap(boolean pageSnap) {
14881505
this.pageSnap = pageSnap;
14891506
return this;
@@ -1532,6 +1549,7 @@ public void load() {
15321549
PDFView.this.setSpacing(spacing);
15331550
PDFView.this.setAutoSpacing(autoSpacing);
15341551
PDFView.this.setPageFitPolicy(pageFitPolicy);
1552+
PDFView.this.setFitEachPage(fitEachPage);
15351553
PDFView.this.setPageSnap(pageSnap);
15361554
PDFView.this.setPageFling(pageFling);
15371555

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/PdfFile.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class PdfFile {
5252
/** Scaled page with maximum width */
5353
private SizeF maxWidthPageSize = new SizeF(0, 0);
5454
/** True if scrolling is vertical, else it's horizontal */
55-
private boolean isVertical = true;
55+
private boolean isVertical;
5656
/** Fixed spacing between pages in pixels */
57-
private int spacingPx = 0;
57+
private int spacingPx;
5858
/** Calculate spacing automatically so each page fits on it's own in the center of the view */
5959
private boolean autoSpacing;
6060
/** Calculated offsets for pages */
@@ -64,21 +64,27 @@ class PdfFile {
6464
/** Calculated document length (width or height, depending on swipe mode) */
6565
private float documentLength = 0;
6666
private final FitPolicy pageFitPolicy;
67+
/**
68+
* True if every page should fit separately according to the FitPolicy,
69+
* else the largest page fits and other pages scale relatively
70+
*/
71+
private final boolean fitEachPage;
6772
/**
6873
* The pages the user want to display in order
6974
* (ex: 0, 2, 2, 8, 8, 1, 1, 1)
7075
*/
7176
private int[] originalUserPages;
7277

7378
PdfFile(PdfiumCore pdfiumCore, PdfDocument pdfDocument, FitPolicy pageFitPolicy, Size viewSize, int[] originalUserPages,
74-
boolean isVertical, int spacing, boolean autoSpacing) {
79+
boolean isVertical, int spacing, boolean autoSpacing, boolean fitEachPage) {
7580
this.pdfiumCore = pdfiumCore;
7681
this.pdfDocument = pdfDocument;
7782
this.pageFitPolicy = pageFitPolicy;
7883
this.originalUserPages = originalUserPages;
7984
this.isVertical = isVertical;
8085
this.spacingPx = spacing;
8186
this.autoSpacing = autoSpacing;
87+
this.fitEachPage = fitEachPage;
8288
setup(viewSize);
8389
}
8490

@@ -111,7 +117,7 @@ private void setup(Size viewSize) {
111117
public void recalculatePageSizes(Size viewSize) {
112118
pageSizes.clear();
113119
PageSizeCalculator calculator = new PageSizeCalculator(pageFitPolicy, originalMaxWidthPageSize,
114-
originalMaxHeightPageSize, viewSize);
120+
originalMaxHeightPageSize, viewSize, fitEachPage);
115121
maxWidthPageSize = calculator.getOptimalMaxWidthPageSize();
116122
maxHeightPageSize = calculator.getOptimalMaxHeightPageSize();
117123

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/util/PageSizeCalculator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,31 @@ public class PageSizeCalculator {
2828
private SizeF optimalMaxHeightPageSize;
2929
private float widthRatio;
3030
private float heightRatio;
31+
private boolean fitEachPage;
3132

3233
public PageSizeCalculator(FitPolicy fitPolicy, Size originalMaxWidthPageSize, Size originalMaxHeightPageSize,
33-
Size viewSize) {
34+
Size viewSize, boolean fitEachPage) {
3435
this.fitPolicy = fitPolicy;
3536
this.originalMaxWidthPageSize = originalMaxWidthPageSize;
3637
this.originalMaxHeightPageSize = originalMaxHeightPageSize;
3738
this.viewSize = viewSize;
39+
this.fitEachPage = fitEachPage;
3840
calculateMaxPages();
3941
}
4042

4143
public SizeF calculate(Size pageSize) {
4244
if (pageSize.getWidth() <= 0 || pageSize.getHeight() <= 0) {
4345
return new SizeF(0, 0);
4446
}
47+
float maxWidth = fitEachPage ? viewSize.getWidth() : pageSize.getWidth() * widthRatio;
48+
float maxHeight = fitEachPage ? viewSize.getHeight() : pageSize.getHeight() * heightRatio;
4549
switch (fitPolicy) {
4650
case HEIGHT:
47-
return fitHeight(pageSize, pageSize.getHeight() * heightRatio);
51+
return fitHeight(pageSize, maxHeight);
4852
case BOTH:
49-
return fitBoth(pageSize, pageSize.getWidth() * widthRatio, pageSize.getHeight() * heightRatio);
53+
return fitBoth(pageSize, maxWidth, maxHeight);
5054
default:
51-
return fitWidth(pageSize, pageSize.getWidth() * widthRatio);
55+
return fitWidth(pageSize, maxWidth);
5256
}
5357
}
5458

0 commit comments

Comments
 (0)