Add rotate document option

This commit is contained in:
Tommy-Geenexus 2017-11-14 18:29:10 +01:00 committed by Daniel Micay
parent 6a03e84622
commit 4ba712409a
14 changed files with 48 additions and 5 deletions

View File

@ -5,6 +5,7 @@ let pageRendering = false;
let renderPending = false;
let renderPendingLazy = false;
const canvas = document.getElementById('content');
let orientationDegrees = 0;
let zoomLevel = 100;
let textLayerDiv = document.getElementById("text");
const zoomLevels = [50, 75, 100, 125, 150];
@ -63,10 +64,13 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
newPageNumber = pageNumber;
newZoomLevel = zoomLevels[channel.getZoomLevel()];
console.log("page: " + pageNumber + ", zoom: " + newZoomLevel + ", prerender: " + prerender);
orientationDegrees = channel.getDocumentOrientationDegrees();
console.log("page: " + pageNumber + ", zoom: " + newZoomLevel +
", 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.zoomLevel === newZoomLevel &&
cache.orientationDegrees == orientationDegrees) {
if (useRender) {
cache.splice(i, 1);
cache.push(cached);
@ -89,7 +93,7 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
}
const newCanvas = document.createElement("canvas");
const viewport = page.getViewport(newZoomLevel / 100)
const viewport = page.getViewport(newZoomLevel / 100, orientationDegrees)
const ratio = window.devicePixelRatio;
newCanvas.height = viewport.height * ratio;
newCanvas.width = viewport.width * ratio;
@ -150,6 +154,7 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
cache.push({
pageNumber: pageNumber,
zoomLevel: newZoomLevel,
orientationDegrees: orientationDegrees,
canvas: newCanvas,
textLayerDiv: newTextLayerDiv
});
@ -163,7 +168,8 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
function onRenderPage(lazy) {
if (pageRendering) {
if (newPageNumber === channel.getPage() && newZoomLevel === zoomLevels[channel.getZoomLevel()]) {
if (newPageNumber === channel.getPage() && newZoomLevel === zoomLevels[channel.getZoomLevel()] &&
orientationDegrees === channel.getDocumentOrientationDegrees()) {
useRender = true;
return;
}

View File

@ -86,6 +86,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
public int mPage;
public int mNumPages;
private int mZoomLevel = 2;
private int mDocumentOrientationDegrees;
private int mDocumentState;
private List<CharSequence> mDocumentProperties;
private InputStream mInputStream;
@ -105,6 +106,11 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
return mZoomLevel;
}
@JavascriptInterface
public int getDocumentOrientationDegrees() {
return mDocumentOrientationDegrees;
}
@JavascriptInterface
public void setNumPages(int numPages) {
mNumPages = numPages;
@ -274,6 +280,14 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
mWebView.evaluateJavascript(lazy ? "onRenderPage(true)" : "onRenderPage(false)", null);
}
private void documentOrientationChanged(final int orientationDegreesOffset) {
mDocumentOrientationDegrees = (mDocumentOrientationDegrees + orientationDegreesOffset) % 360;
if (mDocumentOrientationDegrees < 0) {
mDocumentOrientationDegrees += 360;
}
renderPage(true);
}
private void openDocument() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
@ -345,7 +359,8 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final int ids[] = { R.id.action_zoom_in, R.id.action_zoom_out, R.id.action_jump_to_page,
R.id.action_next, R.id.action_previous, R.id.action_view_document_properties };
R.id.action_next, R.id.action_previous, R.id.action_rotate_clockwise,
R.id.action_rotate_counterclockwise, R.id.action_view_document_properties };
if (mDocumentState < STATE_LOADED) {
for (final int id : ids) {
final MenuItem item = menu.findItem(id);
@ -416,6 +431,14 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
}
return true;
case R.id.action_rotate_clockwise:
documentOrientationChanged(90);
return true;
case R.id.action_rotate_counterclockwise:
documentOrientationChanged(-90);
return true;
case R.id.action_view_document_properties:
DocumentPropertiesFragment
.newInstance(mDocumentProperties)

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 427 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 536 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 535 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 798 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 787 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -43,6 +43,18 @@
android:title="@string/action_jump_to_page"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_rotate_clockwise"
android:icon="@drawable/ic_rotate_right_white_24dp"
android:title="@string/action_rotate_clockwise"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_rotate_counterclockwise"
android:icon="@drawable/ic_rotate_left_white_24dp"
android:title="@string/action_rotate_counterclockwise"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_view_document_properties"
android:title="@string/action_view_document_properties"

View File

@ -7,6 +7,8 @@
<string name="action_open">Open document</string>
<string name="action_zoom_out">Zoom out</string>
<string name="action_zoom_in">Zoom in</string>
<string name="action_rotate_clockwise">Rotate clockwise</string>
<string name="action_rotate_counterclockwise">Rotate counterclockwise</string>
<string name="action_jump_to_page">Jump to page</string>
<string name="action_view_document_properties">Properties</string>