Skip to content

Commit

Permalink
Add Android double tap zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasjunior committed Mar 9, 2023
1 parent 4aa0197 commit f3aa3e4
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 38 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![npm version](https://img.shields.io/npm/v/react-native-pdf-renderer.svg)](https://www.npmjs.com/package/react-native-pdf-renderer)
[![npm downloads](https://img.shields.io/npm/dt/react-native-pdf-renderer.svg)](#install-with-react-native-link)

⚛ A blazing fast, zero dependencies, pure native PDF Renderer for Android and iOS.
⚛ A blazing fast, zero dependencies, pure native, typed PDF Renderer for Android and iOS.

|Android|iOS|
|-|-|
Expand Down Expand Up @@ -38,15 +38,17 @@ import PdfRendererView from 'react-native-pdf-renderer';

const App = () => {
return (
<PdfRendererView
style={{flex: 1}}
source="file:///path/to/local/file.pdf"
distanceBetweenPages={16}
maxZoom={5}
onPageChange={(current, total) => {
console.log(current, total);
}}
/>
<SafeAreaView style={{flex: 1}}>
<PdfRendererView
style={{backgroundColor: 'red'}}
source="file:///path/to/local/file.pdf"
distanceBetweenPages={16}
maxZoom={5}
onPageChange={(current, total) => {
console.log(current, total);
}}
/>
</SafeAreaView>
);
}

Expand Down
2 changes: 1 addition & 1 deletion Sample/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function App(): JSX.Element {
return (
<>
<PdfRendererView
style={{flex: 1, backgroundColor: 'gray'}}
style={{backgroundColor: 'red'}}
source={source}
distanceBetweenPages={16}
maxZoom={2}
Expand Down
2 changes: 1 addition & 1 deletion Sample/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ SPEC CHECKSUMS:
React-RCTVibration: ad17efcfb2fa8f6bfd8ac0cf48d96668b8b28e0b
React-runtimeexecutor: 8fa50b38df6b992c76537993a2b0553d3b088004
ReactCommon: b49a4b00ca6d181ff74b17c12b2d59ac4add0bde
ReactNativePdfRenderer: e16e7bad8100055c7787f468ae5c5b1f6cd1b570
ReactNativePdfRenderer: 1b38752eff13afed18c5a0c545cb1ec03df058b9
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
Yoga: 79dd7410de6f8ad73a77c868d3d368843f0c93e0
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import android.graphics.Matrix;
import android.graphics.pdf.PdfRenderer;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
Expand All @@ -50,12 +49,12 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class PdfRendererRecyclerView extends RecyclerView {
private final ViewGroup mParent;
private final GestureDetector mGestureDetector;
private boolean mRequestedLayout = false;
private float mMinZoom = 1;
private float mMaxZoom = 5;
private float mDistanceBetweenPages = 0;
private int mWidth;
Expand Down Expand Up @@ -150,7 +149,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w,h,oldw,oldh);
super.onSizeChanged(w, h, oldw, oldh);
float scale = Math.max(w / mWidth, h / mHeight);
mMatrix.setScale(scale, scale);
mMatrix.postTranslate((w - scale * mWidth) / 2f, (h - scale * mHeight) / 2f);
Expand All @@ -161,9 +160,6 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
public boolean onTouchEvent(MotionEvent e) {
mScaleDetector.onTouchEvent(e);
mGestureDetector.onTouchEvent(e);
if (mScaleDetector.isInProgress()) {
return true;
}
return super.onTouchEvent(e);
}

Expand Down Expand Up @@ -192,14 +188,11 @@ private void validateMatrixLimits() {
float[] values = new float[9];
mMatrix.getValues(values);

Log.d("mMatrix1", Arrays.toString(values) + "");
Log.d("size", mWidth + " | " + mHeight);

float scaleX = Math.min(Math.max(values[0], 1), mMaxZoom);
float posX = values[2];
float scaleX = Math.min(Math.max(values[Matrix.MSCALE_X], mMinZoom), mMaxZoom);
float posX = values[Matrix.MTRANS_X];

float scaleY = Math.min(Math.max(values[4], 1), mMaxZoom);
float posY = values[5];
float scaleY = Math.min(Math.max(values[Matrix.MSCALE_Y], mMinZoom), mMaxZoom);
float posY = values[Matrix.MTRANS_Y];

float maxPosX = mWidth - mWidth * scaleX;
float maxPosY = mHeight - mHeight * scaleY;
Expand All @@ -214,14 +207,12 @@ else if (posX < maxPosX)
else if (posY < maxPosY)
posY = maxPosY;

values[0] = scaleX;
values[2] = posX;
values[4] = scaleY;
values[5] = posY;
values[Matrix.MSCALE_X] = scaleX;
values[Matrix.MTRANS_X] = posX;
values[Matrix.MSCALE_Y] = scaleY;
values[Matrix.MTRANS_Y] = posY;

mMatrix.setValues(values);

Log.d("mMatrix2", Arrays.toString(values) + "");
}

/**
Expand All @@ -243,7 +234,16 @@ public void requestLayout() {
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float[] values = new float[9];
mMatrix.getValues(values);

float zoom = values[Matrix.MSCALE_X];
float factor = detector.getScaleFactor();

if (zoom >= mMaxZoom && factor > 1) {
return false;
}

mMatrix.postScale(factor, factor, getWidth() / 2f, getHeight() / 2f);
validateMatrixLimits();
ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this);
Expand All @@ -259,6 +259,29 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float dX, float dY) {
ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this);
return true;
}

@Override
public boolean onDoubleTap(@NonNull MotionEvent e) {
if (e.getPointerCount() > 1) {
return false;
}

float[] values = new float[9];
mMatrix.getValues(values);

float currentZoom = values[Matrix.MSCALE_X];
float newZoom = currentZoom > mMinZoom ? mMinZoom : mMaxZoom;
float centerX = getWidth() / 2f;
float centerY = getHeight() / 2f;

mMatrix.setScale(newZoom, newZoom, centerX, centerY);
mMatrix.postTranslate(centerX - e.getX(), centerY - e.getY());

validateMatrixLimits();

ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this);
return true;
}
}

class PdfRendererAdapter extends Adapter<PdfRendererAdapter.ViewHolder> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

package com.github.douglasjunior.reactNativePdfRenderer.modules;

import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

Expand Down Expand Up @@ -83,7 +82,6 @@ public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {

@ReactProp(name = "source")
public void setSource(ViewGroup layout, @Nullable String source) throws IOException {
Log.d("PdfRendererViewManager", "setSource");
PdfRendererRecyclerView recyclerView = (PdfRendererRecyclerView) layout.getChildAt(0);

if (source != null) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "react-native-pdf-renderer",
"displayName": "React-Native Pdf Renderer",
"version": "0.0.5",
"description": "⚛ A blazing fast, zero dependencies, pure native PDF Renderer for Android and iOS.",
"version": "1.0.0",
"description": "⚛ A blazing fast, zero dependencies, pure native, typed PDF Renderer for Android and iOS.",
"main": "src",
"license": "MIT",
"private": false,
Expand Down
19 changes: 16 additions & 3 deletions src/PdfRendererView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import React, {useCallback} from 'react';
import React, {useCallback, useMemo} from 'react';
import {
NativeSyntheticEvent,
requireNativeComponent,
StyleSheet,
ViewProps,
} from 'react-native';

Expand All @@ -41,8 +42,20 @@ type OnPageChangeEventType = {

const PdfRendererNative = requireNativeComponent('RNPdfRendererView') as any;

const styles = StyleSheet.create({
default: {
backgroundColor: 'gray',
flex: 1,
}
});

const PdfRendererView = (props: PdfRendererViewPropsType): JSX.Element => {
const {onPageChange, ...others} = props;
const {onPageChange, style, ...others} = props;

const viewStyles = useMemo(() => [
styles.default,
style,
], [style]);

const handlePageChange = useCallback(
(event: NativeSyntheticEvent<OnPageChangeEventType>) => {
Expand All @@ -51,7 +64,7 @@ const PdfRendererView = (props: PdfRendererViewPropsType): JSX.Element => {
[onPageChange],
);

return <PdfRendererNative {...others} onPageChange={handlePageChange} />;
return <PdfRendererNative {...others} style={viewStyles} onPageChange={handlePageChange} />;
};

PdfRendererView.defaultProps = {
Expand Down

0 comments on commit f3aa3e4

Please sign in to comment.