Skip to content

Commit

Permalink
feat: 更新 pdf-view 默認縮放級別,優化縮放使默認顯示效果更佳清晰
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoKam committed Aug 28, 2023
1 parent 40a5678 commit 2c431bb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
6 changes: 6 additions & 0 deletions packages/pdf-viewer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @orca-fe/pdf-viewer

## 1.18.0

### Minor Changes

- feat: 更新 pdf-view 默認縮放級別,優化縮放使默認顯示效果更佳清晰

## 1.17.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/pdf-viewer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orca-fe/pdf-viewer",
"version": "1.17.1",
"version": "1.18.0",
"description": "PDF Viewer",
"keywords": [
"react",
Expand Down
10 changes: 4 additions & 6 deletions packages/pdf-viewer/src/PDFPage/PDFPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { floorBy } from '@orca-fe/tools';
import type { PageViewport } from '../context';
import PDFViewerContext from '../context';
import useStyle from './PDFPage.style';
import { PixelsPerInch } from '../utils';

const globalOutputScale = Math.max(window.devicePixelRatio, 1);

Expand All @@ -26,7 +27,7 @@ const PDFPage = (props: PdfPageProps) => {

const rootRef = useRef<HTMLDivElement>(null);

const scale = 2 ** zoom;
const scale = 2 ** zoom * PixelsPerInch.PDF_TO_CSS_UNITS;

const [_this] = useState<{
task?: any;
Expand Down Expand Up @@ -62,11 +63,8 @@ const PDFPage = (props: PdfPageProps) => {

if (!render) return;
if (canvas && page) {
// const viewport = page.getViewport({
// scale: Math.min(4 * outputScale, scale),
// }) as PageViewport;
canvas.width = Math.round(viewport.width * outputScale);
canvas.height = Math.round(viewport.height * outputScale);
canvas.width = Math.ceil(viewport.width * outputScale);
canvas.height = Math.ceil(viewport.height * outputScale);
canvas.hidden = true;
const context = canvas.getContext('2d', { alpha: false });
if (context) {
Expand Down
14 changes: 7 additions & 7 deletions packages/pdf-viewer/src/PDFViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DownloadOutlined } from '@ant-design/icons';
import type { PageViewport, PDFViewerHandle, RenderPageCoverFnType, PDFViewerInternalStateType, SourceType } from './context';
import PDFViewerContext, { PDFToolbarContext } from './context';
import PDFPage from './PDFPage';
import { findSortedArr } from './utils';
import { findSortedArr, PixelsPerInch } from './utils';
import ZoomAndPageController from './ZoomAndPageController';
import PDFToolbar from './PDFToolbar';
import useStyle from './PDFViewer.style';
Expand Down Expand Up @@ -192,7 +192,7 @@ const PDFViewer = React.forwardRef<PDFViewerHandle, PDFViewerProps>((props, pRef
setZoom(newZoom);
const pageDom = pageContainerRef.current;
if (pageDom) {
pageDom.style.setProperty('--scale-factor', `${2 ** newZoom}`);
pageDom.style.setProperty('--scale-factor', `${2 ** newZoom * PixelsPerInch.PDF_TO_CSS_UNITS}`);
}
dom.scrollTop = newScrollTop;
dom.scrollLeft = newScrollLeft;
Expand Down Expand Up @@ -240,9 +240,9 @@ const PDFViewer = React.forwardRef<PDFViewerHandle, PDFViewerProps>((props, pRef
if (zoomMode && _this.size && maxWidth && pageMaxHeight) {
if (zoomMode === 'autoWidth') {
// 调整缩放级别,使其与容器宽度匹配
newZoom = Math.log2((_this.size.width - 32) / pageMaxWidth);
newZoom = Math.log2((_this.size.width - 32) / PixelsPerInch.PDF_TO_CSS_UNITS / pageMaxWidth);
} else if (zoomMode === 'autoHeight') {
newZoom = Math.log2((_this.size.height - 32) / pageMaxHeight);
newZoom = Math.log2((_this.size.height - 32) / PixelsPerInch.PDF_TO_CSS_UNITS / pageMaxHeight);
}
}

Expand Down Expand Up @@ -501,7 +501,7 @@ const PDFViewer = React.forwardRef<PDFViewerHandle, PDFViewerProps>((props, pRef

const pageDom = pageContainerRef.current;
if (pageDom) {
pageDom.style.setProperty('--scale-factor', `${2 ** newZoom}`);
pageDom.style.setProperty('--scale-factor', `${2 ** newZoom * PixelsPerInch.PDF_TO_CSS_UNITS}`);
}
dom.scrollTop = newScrollTop;
dom.scrollLeft = newScrollLeft;
Expand Down Expand Up @@ -697,8 +697,8 @@ const PDFViewer = React.forwardRef<PDFViewerHandle, PDFViewerProps>((props, pRef
}
}}
style={{
'--scale-factor': scale,
'--pdf-viewer-page-scale': scale,
'--scale-factor': scale * PixelsPerInch.PDF_TO_CSS_UNITS,
'--pdf-viewer-page-scale': scale * PixelsPerInch.PDF_TO_CSS_UNITS,
}}
>
{viewports.length === 0 && !loading && !pluginLoading && emptyTips}
Expand Down
15 changes: 9 additions & 6 deletions packages/pdf-viewer/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export function findSortedArr(
arr: number[],
value: number,
start = 0,
end = arr.length,
) {
export function findSortedArr(arr: number[], value: number, start = 0, end = arr.length) {
const index = Math.floor((start + end) / 2);
if (arr[end - 1] < value) return end;
if (index === start) {
Expand All @@ -20,3 +15,11 @@ export function findSortedArr(
if (arr[index - 1] < value) return index;
return findSortedArr(arr, value, start, index);
}

export class PixelsPerInch {
static CSS = 96.0;

static PDF = 72.0;

static PDF_TO_CSS_UNITS = this.CSS / this.PDF;
}

1 comment on commit 2c431bb

@vercel
Copy link

@vercel vercel bot commented on 2c431bb Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.