Skip to content

Commit ddeb270

Browse files
committedOct 27, 2024··
feat: get page size
1 parent 2051484 commit ddeb270

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed
 

‎packages/pdf-viewer/__tests__/lorem-ipsum.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ test('pdf lorem ipsum', async () => {
3636
expect(page.width()).toBeCloseTo(595, 0.1);
3737
expect(page.height()).toBeCloseTo(842, 0.1);
3838

39+
expect(page.size()).toStrictEqual({
40+
width: page.width(),
41+
height: page.height(),
42+
});
43+
3944
page.close();
4045
expect(page.pointer).toBe(0);
4146

4247
expect(page.width()).toBe(0);
4348
expect(page.height()).toBe(0);
49+
4450
expect(page.rotation()).toBe(-1);
4551
expect(page.hasTransparency()).toBe(false);
4652

@@ -49,6 +55,12 @@ test('pdf lorem ipsum', async () => {
4955

5056
expect(page.width()).toBeCloseTo(595, 0.1);
5157
expect(page.height()).toBeCloseTo(842, 0.1);
58+
59+
expect(page.size()).toStrictEqual({
60+
width: page.width(),
61+
height: page.height(),
62+
});
63+
5264
expect(page.rotation()).toBe(0);
5365
expect(page.hasTransparency()).toBe(false);
5466

‎packages/pdf-viewer/__tests__/minimal.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ test('pdf minimal', async () => {
3636
expect(page.width()).toBeCloseTo(595, 0.1);
3737
expect(page.height()).toBeCloseTo(842, 0.1);
3838

39+
expect(page.size()).toStrictEqual({
40+
width: page.width(),
41+
height: page.height(),
42+
});
43+
3944
page.close();
4045
expect(page.pointer).toBe(0);
4146

4247
expect(page.width()).toBe(0);
4348
expect(page.height()).toBe(0);
49+
4450
expect(page.rotation()).toBe(-1);
4551
expect(page.hasTransparency()).toBe(false);
4652

@@ -49,6 +55,12 @@ test('pdf minimal', async () => {
4955

5056
expect(page.width()).toBeCloseTo(595, 0.1);
5157
expect(page.height()).toBeCloseTo(842, 0.1);
58+
59+
expect(page.size()).toStrictEqual({
60+
width: page.width(),
61+
height: page.height(),
62+
});
63+
5264
expect(page.rotation()).toBe(0);
5365
expect(page.hasTransparency()).toBe(false);
5466

‎packages/pdf-viewer/src/document.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { FileIdentifier, MetaTag, Metadata } from '@toeverything/pdf-viewer-types';
1+
import type {
2+
FileIdentifier,
3+
MetaTag,
4+
Metadata,
5+
} from '@toeverything/pdf-viewer-types';
26
import { MetaTags } from '@toeverything/pdf-viewer-types';
37

48
import type { Runtime } from './runtime.js';

‎packages/pdf-viewer/src/page.ts

+31-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { Bitmap } from './bitmap.js';
22
import type { Document } from './document.js';
33

44
export class Page {
5+
#width = 0;
6+
#height = 0;
7+
58
constructor(
69
public doc: Document,
710
public index: number,
@@ -31,6 +34,9 @@ export class Page {
3134
if (!this.ptr) return;
3235
this.runtime.closePage(this.ptr);
3336
this.ptr = 0;
37+
38+
this.#width = 0;
39+
this.#height = 0;
3440
}
3541

3642
label() {
@@ -57,11 +63,17 @@ export class Page {
5763
}
5864

5965
width() {
60-
return this.runtime.pageWidth(this.ptr);
66+
if (!this.#width) {
67+
this.#width = this.runtime.pageWidth(this.ptr);
68+
}
69+
return this.#width;
6170
}
6271

6372
height() {
64-
return this.runtime.pageHeight(this.ptr);
73+
if (!this.#height) {
74+
this.#height = this.runtime.pageHeight(this.ptr);
75+
}
76+
return this.#height;
6577
}
6678

6779
rotation() {
@@ -72,8 +84,24 @@ export class Page {
7284
return !!this.runtime.pageTransparency(this.ptr);
7385
}
7486

87+
size() {
88+
if (!this.#width || !this.#height) {
89+
const sizePtr = this.runtime.malloc(8);
90+
91+
this.runtime.pageSize(this.doc.pointer, this.index, sizePtr);
92+
93+
this.#width = this.runtime.getValue(sizePtr, 'float');
94+
this.#height = this.runtime.getValue(sizePtr + 4, 'float');
95+
96+
this.runtime.free(sizePtr);
97+
}
98+
99+
return { width: this.#width, height: this.#height };
100+
}
101+
75102
rect() {
76-
return { bottom: 0, left: 0, top: this.height(), right: this.width() };
103+
const { width: right, height: top } = this.size();
104+
return { bottom: 0, left: 0, top, right };
77105
}
78106

79107
render(

‎packages/pdf-viewer/src/runtime.ts

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class Runtime {
105105
pageLabel = this.wasm.FPDF_GetPageLabel;
106106
pageWidth = this.wasm.FPDF_GetPageWidthF;
107107
pageHeight = this.wasm.FPDF_GetPageHeightF;
108+
pageSize = this.wasm.FPDF_GetPageSizeByIndexF;
108109
pageRotation = this.wasm.FPDFPage_GetRotation<Rotation>;
109110
pageTransparency = this.wasm.FPDFPage_HasTransparency;
110111
renderPageBitmap = this.wasm.FPDF_RenderPageBitmap;

‎packages/pdfium/dist/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface FPDF_Bindings {
3838
size?: number
3939
): number;
4040
FPDF_GetPageSizeByIndexF(
41-
docPtr: numbern,
41+
docPtr: number,
4242
pageIdx: number,
4343
rectPtr: number
4444
): boolean;

0 commit comments

Comments
 (0)
Please sign in to comment.