Preparation for ratio zoom

This commit is contained in:
Antoine d'Aligny 2019-11-18 11:49:58 +01:00 committed by Daniel Micay
parent f7289be8a5
commit e509660d7e
2 changed files with 26 additions and 35 deletions

View File

@ -7,13 +7,12 @@ let renderPending = false;
let renderPendingLazy = false;
const canvas = document.getElementById('content');
let orientationDegrees = 0;
let zoomLevel = 100;
let zoomRatio = 1;
let textLayerDiv = document.getElementById("text");
const zoomLevels = [50, 75, 100, 125, 150];
let task = null;
let newPageNumber = 0;
let newZoomLevel = 0;
let newZoomRatio = 1;
let useRender;
const cache = [];
@ -65,13 +64,13 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
useRender = !prerender;
newPageNumber = pageNumber;
newZoomLevel = zoomLevels[channel.getZoomLevel()];
newZoomRatio = channel.getZoomRatio();
orientationDegrees = channel.getDocumentOrientationDegrees();
console.log("page: " + pageNumber + ", zoom: " + newZoomLevel +
console.log("page: " + pageNumber + ", zoom: " + newZoomRatio +
", orientationDegrees: " + orientationDegrees + ", prerender: " + prerender);
for (let i = 0; i < cache.length; i++) {
const cached = cache[i];
if (cached.pageNumber === pageNumber && cached.zoomLevel === newZoomLevel &&
if (cached.pageNumber === pageNumber && cached.zoomRatio === newZoomRatio &&
cache.orientationDegrees === orientationDegrees) {
if (useRender) {
cache.splice(i, 1);
@ -95,7 +94,7 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
}
const newCanvas = document.createElement("canvas");
const viewport = page.getViewport({scale: newZoomLevel / 100, rotation: orientationDegrees})
const viewport = page.getViewport({scale: newZoomRatio, rotation: orientationDegrees})
const ratio = window.devicePixelRatio;
newCanvas.height = viewport.height * ratio;
newCanvas.width = viewport.width * ratio;
@ -105,11 +104,11 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
newContext.scale(ratio, ratio);
if (useRender) {
if (newZoomLevel !== zoomLevel) {
if (newZoomRatio !== zoomRatio) {
canvas.style.height = viewport.height + "px";
canvas.style.width = viewport.width + "px";
}
zoomLevel = newZoomLevel;
zoomRatio = newZoomRatio;
}
task = page.render({
@ -155,7 +154,7 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
}
cache.push({
pageNumber: pageNumber,
zoomLevel: newZoomLevel,
zoomRatio: newZoomRatio,
orientationDegrees: orientationDegrees,
canvas: newCanvas,
textLayerDiv: newTextLayerDiv
@ -170,7 +169,7 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
function onRenderPage(lazy) {
if (pageRendering) {
if (newPageNumber === channel.getPage() && newZoomLevel === zoomLevels[channel.getZoomLevel()] &&
if (newPageNumber === channel.getPage() && getZoomRatio === channel.getZoomRatio() &&
orientationDegrees === channel.getDocumentOrientationDegrees()) {
useRender = true;
return;

View File

@ -41,7 +41,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private static final String STATE_URI = "uri";
private static final String STATE_PAGE = "page";
private static final String STATE_ZOOM_LEVEL = "zoomLevel";
private static final String STATE_ZOOM_RATIO = "zoomRatio";
private static final String STATE_DOCUMENT_ORIENTATION_DEGREES = "documentOrientationDegrees";
private static final String KEY_PROPERTIES = "properties";
@ -74,8 +74,8 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
"usb 'none'; " +
"vr 'none'";
private static final int MIN_ZOOM_LEVEL = 0;
private static final int MAX_ZOOM_LEVEL = 4;
private static final float MIN_ZOOM_RATIO = 0.5f;
private static final float MAX_ZOOM_RATIO = 1.5f;
private static final int ALPHA_LOW = 130;
private static final int ALPHA_HIGH = 255;
private static final int ACTION_OPEN_DOCUMENT_REQUEST_CODE = 1;
@ -86,7 +86,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private Uri mUri;
public int mPage;
public int mNumPages;
private int mZoomLevel = 2;
private float mZoomRatio = 1f;
private int mDocumentOrientationDegrees;
private int mDocumentState;
private int windowInsetTop;
@ -109,8 +109,8 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
}
@JavascriptInterface
public int getZoomLevel() {
return mZoomLevel;
public float getZoomRatio() {
return mZoomRatio;
}
@JavascriptInterface
@ -292,7 +292,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
if (savedInstanceState != null) {
mUri = savedInstanceState.getParcelable(STATE_URI);
mPage = savedInstanceState.getInt(STATE_PAGE);
mZoomLevel = savedInstanceState.getInt(STATE_ZOOM_LEVEL);
mZoomRatio = savedInstanceState.getFloat(STATE_ZOOM_RATIO);
mDocumentOrientationDegrees = savedInstanceState.getInt(STATE_DOCUMENT_ORIENTATION_DEGREES);
}
@ -349,16 +349,16 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
}
private void zoomIn() {
if (mZoomLevel < MAX_ZOOM_LEVEL) {
mZoomLevel++;
if (mZoomRatio < MAX_ZOOM_RATIO) {
mZoomRatio+=0.25f;
renderPage(true);
invalidateOptionsMenu();
}
}
private void zoomOut() {
if (mZoomLevel > 0) {
mZoomLevel--;
if (mZoomRatio > MIN_ZOOM_RATIO) {
mZoomRatio-=0.25f;
renderPage(true);
invalidateOptionsMenu();
}
@ -406,7 +406,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelable(STATE_URI, mUri);
savedInstanceState.putInt(STATE_PAGE, mPage);
savedInstanceState.putInt(STATE_ZOOM_LEVEL, mZoomLevel);
savedInstanceState.putFloat(STATE_ZOOM_RATIO, mZoomRatio);
savedInstanceState.putInt(STATE_DOCUMENT_ORIENTATION_DEGREES, mDocumentOrientationDegrees);
}
@ -468,18 +468,10 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
mDocumentState = STATE_END;
}
switch (mZoomLevel) {
case MAX_ZOOM_LEVEL:
enableDisableMenuItem(menu.findItem(R.id.action_zoom_in), false);
return true;
case MIN_ZOOM_LEVEL:
enableDisableMenuItem(menu.findItem(R.id.action_zoom_out), false);
return true;
default:
enableDisableMenuItem(menu.findItem(R.id.action_zoom_in), true);
enableDisableMenuItem(menu.findItem(R.id.action_zoom_out), true);
return true;
}
enableDisableMenuItem(menu.findItem(R.id.action_zoom_in), mZoomRatio != MAX_ZOOM_RATIO);
enableDisableMenuItem(menu.findItem(R.id.action_zoom_out), mZoomRatio != MIN_ZOOM_RATIO);
return true;
}
@Override