set zoomRatio to fit document to screen size

This commit is contained in:
Hanouta 2023-06-09 21:10:57 +02:00 committed by Daniel Micay
parent a820314362
commit 0060e2cb73
2 changed files with 30 additions and 0 deletions

View File

@ -72,6 +72,13 @@ function setLayerTransform(pageWidth, pageHeight, layerDiv) {
layerDiv.style.translate = `${translate.X}px ${translate.Y}px`;
}
function getDefaultZoomRatio(page, orientationDegrees) {
const viewport = page.getViewport({scale: 1, rotation: orientationDegrees});
const widthZoomRatio = document.body.clientWidth / viewport.width;
const heightZoomRatio = document.body.clientHeight / viewport.height;
return Math.max(Math.min(widthZoomRatio, heightZoomRatio, channel.getMaxZoomRatio()), channel.getMinZoomRatio());
}
function renderPage(pageNumber, zoom, prerender, prerenderTrigger=0) {
pageRendering = true;
useRender = !prerender;
@ -108,6 +115,14 @@ function renderPage(pageNumber, zoom, prerender, prerenderTrigger=0) {
return;
}
const defaultZoomRatio = getDefaultZoomRatio(page, orientationDegrees);
if (cache.length === 0) {
zoomRatio = defaultZoomRatio;
newZoomRatio = defaultZoomRatio;
channel.setZoomRatio(defaultZoomRatio);
}
const viewport = page.getViewport({scale: newZoomRatio, rotation: orientationDegrees});
if (useRender) {

View File

@ -172,6 +172,21 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
return mZoomRatio;
}
@JavascriptInterface
public void setZoomRatio(final float ratio) {
mZoomRatio = Math.max(Math.min(ratio, MAX_ZOOM_RATIO), MIN_ZOOM_RATIO);
}
@JavascriptInterface
public float getMinZoomRatio() {
return MIN_ZOOM_RATIO;
}
@JavascriptInterface
public float getMaxZoomRatio() {
return MAX_ZOOM_RATIO;
}
@JavascriptInterface
public int getDocumentOrientationDegrees() {
return mDocumentOrientationDegrees;