Add rotate document option
@ -5,6 +5,7 @@ let pageRendering = false;
|
|||||||
let renderPending = false;
|
let renderPending = false;
|
||||||
let renderPendingLazy = false;
|
let renderPendingLazy = false;
|
||||||
const canvas = document.getElementById('content');
|
const canvas = document.getElementById('content');
|
||||||
|
let orientationDegrees = 0;
|
||||||
let zoomLevel = 100;
|
let zoomLevel = 100;
|
||||||
let textLayerDiv = document.getElementById("text");
|
let textLayerDiv = document.getElementById("text");
|
||||||
const zoomLevels = [50, 75, 100, 125, 150];
|
const zoomLevels = [50, 75, 100, 125, 150];
|
||||||
@ -63,10 +64,13 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
|
|||||||
|
|
||||||
newPageNumber = pageNumber;
|
newPageNumber = pageNumber;
|
||||||
newZoomLevel = zoomLevels[channel.getZoomLevel()];
|
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++) {
|
for (let i = 0; i < cache.length; i++) {
|
||||||
const cached = cache[i];
|
const cached = cache[i];
|
||||||
if (cached.pageNumber === pageNumber && cached.zoomLevel === newZoomLevel) {
|
if (cached.pageNumber === pageNumber && cached.zoomLevel === newZoomLevel &&
|
||||||
|
cache.orientationDegrees == orientationDegrees) {
|
||||||
if (useRender) {
|
if (useRender) {
|
||||||
cache.splice(i, 1);
|
cache.splice(i, 1);
|
||||||
cache.push(cached);
|
cache.push(cached);
|
||||||
@ -89,7 +93,7 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const newCanvas = document.createElement("canvas");
|
const newCanvas = document.createElement("canvas");
|
||||||
const viewport = page.getViewport(newZoomLevel / 100)
|
const viewport = page.getViewport(newZoomLevel / 100, orientationDegrees)
|
||||||
const ratio = window.devicePixelRatio;
|
const ratio = window.devicePixelRatio;
|
||||||
newCanvas.height = viewport.height * ratio;
|
newCanvas.height = viewport.height * ratio;
|
||||||
newCanvas.width = viewport.width * ratio;
|
newCanvas.width = viewport.width * ratio;
|
||||||
@ -150,6 +154,7 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
|
|||||||
cache.push({
|
cache.push({
|
||||||
pageNumber: pageNumber,
|
pageNumber: pageNumber,
|
||||||
zoomLevel: newZoomLevel,
|
zoomLevel: newZoomLevel,
|
||||||
|
orientationDegrees: orientationDegrees,
|
||||||
canvas: newCanvas,
|
canvas: newCanvas,
|
||||||
textLayerDiv: newTextLayerDiv
|
textLayerDiv: newTextLayerDiv
|
||||||
});
|
});
|
||||||
@ -163,7 +168,8 @@ function renderPage(pageNumber, lazy, prerender, prerenderTrigger=0) {
|
|||||||
|
|
||||||
function onRenderPage(lazy) {
|
function onRenderPage(lazy) {
|
||||||
if (pageRendering) {
|
if (pageRendering) {
|
||||||
if (newPageNumber === channel.getPage() && newZoomLevel === zoomLevels[channel.getZoomLevel()]) {
|
if (newPageNumber === channel.getPage() && newZoomLevel === zoomLevels[channel.getZoomLevel()] &&
|
||||||
|
orientationDegrees === channel.getDocumentOrientationDegrees()) {
|
||||||
useRender = true;
|
useRender = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -86,6 +86,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
|||||||
public int mPage;
|
public int mPage;
|
||||||
public int mNumPages;
|
public int mNumPages;
|
||||||
private int mZoomLevel = 2;
|
private int mZoomLevel = 2;
|
||||||
|
private int mDocumentOrientationDegrees;
|
||||||
private int mDocumentState;
|
private int mDocumentState;
|
||||||
private List<CharSequence> mDocumentProperties;
|
private List<CharSequence> mDocumentProperties;
|
||||||
private InputStream mInputStream;
|
private InputStream mInputStream;
|
||||||
@ -105,6 +106,11 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
|||||||
return mZoomLevel;
|
return mZoomLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JavascriptInterface
|
||||||
|
public int getDocumentOrientationDegrees() {
|
||||||
|
return mDocumentOrientationDegrees;
|
||||||
|
}
|
||||||
|
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
public void setNumPages(int numPages) {
|
public void setNumPages(int numPages) {
|
||||||
mNumPages = numPages;
|
mNumPages = numPages;
|
||||||
@ -274,6 +280,14 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
|||||||
mWebView.evaluateJavascript(lazy ? "onRenderPage(true)" : "onRenderPage(false)", null);
|
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() {
|
private void openDocument() {
|
||||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
@ -345,7 +359,8 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
|||||||
@Override
|
@Override
|
||||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||||
final int ids[] = { R.id.action_zoom_in, R.id.action_zoom_out, R.id.action_jump_to_page,
|
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) {
|
if (mDocumentState < STATE_LOADED) {
|
||||||
for (final int id : ids) {
|
for (final int id : ids) {
|
||||||
final MenuItem item = menu.findItem(id);
|
final MenuItem item = menu.findItem(id);
|
||||||
@ -416,6 +431,14 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
|||||||
}
|
}
|
||||||
return true;
|
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:
|
case R.id.action_view_document_properties:
|
||||||
DocumentPropertiesFragment
|
DocumentPropertiesFragment
|
||||||
.newInstance(mDocumentProperties)
|
.newInstance(mDocumentProperties)
|
||||||
|
BIN
app/src/main/res/drawable-hdpi/ic_rotate_left_white_24dp.png
Normal file
After Width: | Height: | Size: 433 B |
BIN
app/src/main/res/drawable-hdpi/ic_rotate_right_white_24dp.png
Normal file
After Width: | Height: | Size: 427 B |
BIN
app/src/main/res/drawable-mdpi/ic_rotate_left_white_24dp.png
Normal file
After Width: | Height: | Size: 271 B |
BIN
app/src/main/res/drawable-mdpi/ic_rotate_right_white_24dp.png
Normal file
After Width: | Height: | Size: 272 B |
BIN
app/src/main/res/drawable-xhdpi/ic_rotate_left_white_24dp.png
Normal file
After Width: | Height: | Size: 536 B |
BIN
app/src/main/res/drawable-xhdpi/ic_rotate_right_white_24dp.png
Normal file
After Width: | Height: | Size: 535 B |
BIN
app/src/main/res/drawable-xxhdpi/ic_rotate_left_white_24dp.png
Normal file
After Width: | Height: | Size: 798 B |
BIN
app/src/main/res/drawable-xxhdpi/ic_rotate_right_white_24dp.png
Normal file
After Width: | Height: | Size: 787 B |
BIN
app/src/main/res/drawable-xxxhdpi/ic_rotate_left_white_24dp.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
app/src/main/res/drawable-xxxhdpi/ic_rotate_right_white_24dp.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
@ -43,6 +43,18 @@
|
|||||||
android:title="@string/action_jump_to_page"
|
android:title="@string/action_jump_to_page"
|
||||||
app:showAsAction="ifRoom" />
|
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
|
<item
|
||||||
android:id="@+id/action_view_document_properties"
|
android:id="@+id/action_view_document_properties"
|
||||||
android:title="@string/action_view_document_properties"
|
android:title="@string/action_view_document_properties"
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
<string name="action_open">Open document</string>
|
<string name="action_open">Open document</string>
|
||||||
<string name="action_zoom_out">Zoom out</string>
|
<string name="action_zoom_out">Zoom out</string>
|
||||||
<string name="action_zoom_in">Zoom in</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_jump_to_page">Jump to page</string>
|
||||||
<string name="action_view_document_properties">Properties</string>
|
<string name="action_view_document_properties">Properties</string>
|
||||||
|
|
||||||
|