Skip to content

Commit

Permalink
feat: get page size
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Oct 27, 2024
1 parent 2051484 commit ddeb270
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
12 changes: 12 additions & 0 deletions packages/pdf-viewer/__tests__/lorem-ipsum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ test('pdf lorem ipsum', async () => {
expect(page.width()).toBeCloseTo(595, 0.1);
expect(page.height()).toBeCloseTo(842, 0.1);

expect(page.size()).toStrictEqual({
width: page.width(),
height: page.height(),
});

page.close();
expect(page.pointer).toBe(0);

expect(page.width()).toBe(0);
expect(page.height()).toBe(0);

expect(page.rotation()).toBe(-1);
expect(page.hasTransparency()).toBe(false);

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

expect(page.width()).toBeCloseTo(595, 0.1);
expect(page.height()).toBeCloseTo(842, 0.1);

expect(page.size()).toStrictEqual({
width: page.width(),
height: page.height(),
});

expect(page.rotation()).toBe(0);
expect(page.hasTransparency()).toBe(false);

Expand Down
12 changes: 12 additions & 0 deletions packages/pdf-viewer/__tests__/minimal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ test('pdf minimal', async () => {
expect(page.width()).toBeCloseTo(595, 0.1);
expect(page.height()).toBeCloseTo(842, 0.1);

expect(page.size()).toStrictEqual({
width: page.width(),
height: page.height(),
});

page.close();
expect(page.pointer).toBe(0);

expect(page.width()).toBe(0);
expect(page.height()).toBe(0);

expect(page.rotation()).toBe(-1);
expect(page.hasTransparency()).toBe(false);

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

expect(page.width()).toBeCloseTo(595, 0.1);
expect(page.height()).toBeCloseTo(842, 0.1);

expect(page.size()).toStrictEqual({
width: page.width(),
height: page.height(),
});

expect(page.rotation()).toBe(0);
expect(page.hasTransparency()).toBe(false);

Expand Down
6 changes: 5 additions & 1 deletion packages/pdf-viewer/src/document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { FileIdentifier, MetaTag, Metadata } from '@toeverything/pdf-viewer-types';
import type {
FileIdentifier,
MetaTag,
Metadata,
} from '@toeverything/pdf-viewer-types';
import { MetaTags } from '@toeverything/pdf-viewer-types';

import type { Runtime } from './runtime.js';
Expand Down
34 changes: 31 additions & 3 deletions packages/pdf-viewer/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { Bitmap } from './bitmap.js';
import type { Document } from './document.js';

export class Page {
#width = 0;
#height = 0;

constructor(
public doc: Document,
public index: number,
Expand Down Expand Up @@ -31,6 +34,9 @@ export class Page {
if (!this.ptr) return;
this.runtime.closePage(this.ptr);
this.ptr = 0;

this.#width = 0;
this.#height = 0;
}

label() {
Expand All @@ -57,11 +63,17 @@ export class Page {
}

width() {
return this.runtime.pageWidth(this.ptr);
if (!this.#width) {
this.#width = this.runtime.pageWidth(this.ptr);
}
return this.#width;
}

height() {
return this.runtime.pageHeight(this.ptr);
if (!this.#height) {
this.#height = this.runtime.pageHeight(this.ptr);
}
return this.#height;
}

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

size() {
if (!this.#width || !this.#height) {
const sizePtr = this.runtime.malloc(8);

this.runtime.pageSize(this.doc.pointer, this.index, sizePtr);

this.#width = this.runtime.getValue(sizePtr, 'float');
this.#height = this.runtime.getValue(sizePtr + 4, 'float');

this.runtime.free(sizePtr);
}

return { width: this.#width, height: this.#height };
}

rect() {
return { bottom: 0, left: 0, top: this.height(), right: this.width() };
const { width: right, height: top } = this.size();
return { bottom: 0, left: 0, top, right };
}

render(
Expand Down
1 change: 1 addition & 0 deletions packages/pdf-viewer/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class Runtime {
pageLabel = this.wasm.FPDF_GetPageLabel;
pageWidth = this.wasm.FPDF_GetPageWidthF;
pageHeight = this.wasm.FPDF_GetPageHeightF;
pageSize = this.wasm.FPDF_GetPageSizeByIndexF;
pageRotation = this.wasm.FPDFPage_GetRotation<Rotation>;
pageTransparency = this.wasm.FPDFPage_HasTransparency;
renderPageBitmap = this.wasm.FPDF_RenderPageBitmap;
Expand Down
2 changes: 1 addition & 1 deletion packages/pdfium/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface FPDF_Bindings {
size?: number
): number;
FPDF_GetPageSizeByIndexF(
docPtr: numbern,
docPtr: number,
pageIdx: number,
rectPtr: number
): boolean;
Expand Down

0 comments on commit ddeb270

Please sign in to comment.