import type { PdfRuntimeMemory, Ptr } from '@embedpdf/engine-runtime'; /** `FS_RECTF { float left, top, right, bottom }` → 16 bytes. */ export const RECTF_BYTES = 16; /** `FS_SIZEF { float width, height }` → 8 bytes. */ export const SIZEF_BYTES = 8; /** A single 32-bit float (e.g. a user-unit out-parameter). */ export const F32_BYTES = 4; /** A single 32-bit int (e.g. a color channel out-parameter). */ export const I32_BYTES = 4; /** `FS_QUADPOINTSF` — 8 floats → 32 bytes. */ export const QUADPOINTSF_BYTES = 32; /** `FS_POINTF { float x, y }` → 8 bytes. */ export const POINTF_BYTES = 8; /** `FS_MATRIX { float a, b, c, d, e, f }` → 24 bytes. */ export const MATRIXF_BYTES = 24; /** Generated from `EPDF_CHAR_GEOMETRY`; pinned by native static_asserts. */ export const EPDF_CHAR_GEOMETRY_LAYOUT = { bytes: 124, offsets: { looseBox: 0, tightBox: 16, looseQuad: 32, tightQuad: 64, matrix: 96, flags: 120, }, flags: { hasTightBox: 1 << 0, hasLooseQuad: 1 << 1, hasTightQuad: 1 << 2, upright: 1 << 3, space: 1 << 4, empty: 1 << 5, synthesized: 1 << 6, }, } as const; export interface RectF { left: number; top: number; right: number; bottom: number; } export interface SizeF { width: number; height: number; } /** Decode an `FS_RECTF` struct that a native call wrote into `ptr`. */ export function readRectF(mem: PdfRuntimeMemory, ptr: Ptr, byteOffset = 0): RectF { return { left: readF32(mem, ptr, byteOffset), top: readF32(mem, ptr, byteOffset + 4), right: readF32(mem, ptr, byteOffset + 8), bottom: readF32(mem, ptr, byteOffset + 12), }; } /** Encode `rect` as the `FS_RECTF` struct a native call reads from `ptr`. */ export function writeRectF(mem: PdfRuntimeMemory, ptr: Ptr, rect: RectF): void { mem.poke(ptr, 'f32', rect.left, 0); mem.poke(ptr, 'f32', rect.top, 4); mem.poke(ptr, 'f32', rect.right, 8); mem.poke(ptr, 'f32', rect.bottom, 12); } /** An `FS_MATRIX`: `x' = a·x + c·y + e`, `y' = b·x + d·y + f`. */ export interface MatrixF { a: number; b: number; c: number; d: number; e: number; f: number; } /** Decode an `FS_MATRIX` struct that a native call wrote into `ptr`. */ export function readMatrixF(mem: PdfRuntimeMemory, ptr: Ptr): MatrixF { return { a: readF32(mem, ptr, 0), b: readF32(mem, ptr, 4), c: readF32(mem, ptr, 8), d: readF32(mem, ptr, 12), e: readF32(mem, ptr, 16), f: readF32(mem, ptr, 20), }; } /** Decode an `FS_SIZEF` struct that a native call wrote into `ptr`. */ export function readSizeF(mem: PdfRuntimeMemory, ptr: Ptr): SizeF { return { width: readF32(mem, ptr, 0), height: readF32(mem, ptr, 4), }; } /** Read a single 32-bit float at `byteOffset` within `ptr`. */ export function readF32(mem: PdfRuntimeMemory, ptr: Ptr, byteOffset = 0): number { return Number(mem.peek(ptr, 'f32', byteOffset)); } /** Read a single 32-bit int at `byteOffset` within `ptr`. */ export function readI32(mem: PdfRuntimeMemory, ptr: Ptr, byteOffset = 0): number { return Number(mem.peek(ptr, 'i32', byteOffset)); }