# @embedpdf/core-geometry — . Mat2D [TypeAliasDeclaration]: export type Mat2D = readonly [ number, number, number, number, number, number, ] &{ readonly __from?: From; readonly __to?: To; } ; NO_FRAME [VariableDeclaration]: NO_FRAME: PageFrame ={ top: 0, right: 0, bottom: 0, left: 0} PageFrame [InterfaceDeclaration]: export interface PageFrame{ top: number; right: number; bottom: number; left: number; } PagePoint [TypeAliasDeclaration] (from packages/core/geometry/src/page-space.ts): export type PagePoint = PointIn<'page'>; PageQuad [TypeAliasDeclaration] (from packages/core/geometry/src/page-space.ts): export type PageQuad = QuadIn<'page'>; PageRect [TypeAliasDeclaration] (from packages/core/geometry/src/page-space.ts): export type PageRect = RectIn<'page'>; PageTransform [InterfaceDeclaration]: export interface PageTransform{ readonly rotation: Rotation; readonly viewWidth: number; readonly viewHeight: number; readonly contentWidth: number; readonly contentHeight: number; readonly deviceWidth: number; readonly deviceHeight: number; readonly renderScale: number; readonly viewScale: number; readonly baseScale: number; readonly zoom: number; toPixels(point: Point): Point; fromPixels(point: Point): Point; pageToView(point: Point): Point; pageToViewRect(rect: Rect): Rect; viewToPage(point: Point): Point; viewToPageRect(rect: Rect): Rect; readonly cssMatrix: string; } Point [InterfaceDeclaration]: export interface Point{ x: number; y: number; } PointIn [TypeAliasDeclaration]: export type PointIn = Point & SpaceBrand; Quad [InterfaceDeclaration]: export interface Quad{ upperLeft: Point; upperRight: Point; lowerLeft: Point; lowerRight: Point; } QuadIn [TypeAliasDeclaration]: export type QuadIn = Quad & SpaceBrand; QuadRing [TypeAliasDeclaration]: export type QuadRing = [Point, Point, Point, Point]; Rect [InterfaceDeclaration]: export interface Rect{ x: number; y: number; width: number; height: number; } RectIn [TypeAliasDeclaration]: export type RectIn = Rect & SpaceBrand; Rotation [TypeAliasDeclaration]: export type Rotation = 0 | 90 | 180 | 270; Size [InterfaceDeclaration]: export interface Size{ width: number; height: number; } Space [TypeAliasDeclaration]: export type Space = 'page' | 'view' | 'screen'; addRotations [FunctionDeclaration]: export function addRotations(left: Rotation, right: Rotation): Rotation{ return ((left + right) % 360) as Rotation; } angleOf [FunctionDeclaration]: export function angleOf(matrix: Mat2D): number{ return Math.atan2(matrix[1], matrix[0]); } applyPoint [FunctionDeclaration]: export function applyPoint( matrix: Mat2D, point: PointIn, ): PointIn{ const [a, b, c, d, e, f] = matrix; return{ x: a * point.x + c * point.y + e, y: b * point.x + d * point.y + f} as PointIn; } applyQuad [FunctionDeclaration]: export function applyQuad( matrix: Mat2D, quad: QuadIn, ): QuadIn{ return{ upperLeft: applyPoint(matrix, quad.upperLeft as PointIn), upperRight: applyPoint(matrix, quad.upperRight as PointIn), lowerLeft: applyPoint(matrix, quad.lowerLeft as PointIn), lowerRight: applyPoint(matrix, quad.lowerRight as PointIn), } as QuadIn; } applyRect [FunctionDeclaration]: export function applyRect( matrix: Mat2D, rect: RectIn, ): RectIn{ const c0 = applyPoint(matrix, { x: rect.x, y: rect.y} as PointIn); const c1 = applyPoint(matrix, { x: rect.x + rect.width, y: rect.y} as PointIn); const c2 = applyPoint(matrix, { x: rect.x, y: rect.y + rect.height} as PointIn); const c3 = applyPoint(matrix, { x: rect.x + rect.width, y: rect.y + rect.height} as PointIn); const xs = [c0.x, c1.x, c2.x, c3.x]; const ys = [c0.y, c1.y, c2.y, c3.y]; const minX = Math.min(...xs); const minY = Math.min(...ys); return{ x: minX, y: minY, width: Math.max(...xs) - minX, height: Math.max(...ys) - minY, } as RectIn; } boundsOfRects [FunctionDeclaration]: export function boundsOfRects(rects: readonly Rect[]): Rect | null{ if (rects.length === 0) return null; let x1 = Infinity; let y1 = Infinity; let x2 = -Infinity; let y2 = -Infinity; for (const rect of rects){ x1 = Math.min(x1, rect.x); y1 = Math.min(y1, rect.y); x2 = Math.max(x2, rect.x + rect.width); y2 = Math.max(y2, rect.y + rect.height); } return{ x: x1, y: y1, width: x2 - x1, height: y2 - y1} ; } compose [FunctionDeclaration]: export function compose( matrix: Mat2D, inner: Mat2D, ): Mat2D{ const [a, b, c, d, e, f] = matrix; const [a2, b2, c2, d2, e2, f2] = inner; return [ a * a2 + c * b2, b * a2 + d * b2, a * c2 + c * d2, b * c2 + d * d2, a * e2 + c * f2 + e, b * e2 + d * f2 + f, ] as Mat2D; } containedRect [FunctionDeclaration]: export function containedRect(content: Size, box: Rect): Rect{ if (content.width <= 0 || content.height <= 0) return box; const scale = Math.min(box.width / content.width, box.height / content.height); const width = content.width * scale; const height = content.height * scale; return{ x: box.x + (box.width - width) / 2, y: box.y + (box.height - height) / 2, width, height, } ; } deviceHeightForWidth [FunctionDeclaration]: export function deviceHeightForWidth(pageSize: Size, deviceWidth: number): number{ return Math.max(1, Math.round((deviceWidth * pageSize.height) / pageSize.width)); } displaySize [FunctionDeclaration]: export function displaySize(content: Size, rotation: Rotation): Size{ return isQuarterTurn(rotation) ?{ width: content.height, height: content.width} :{ width: content.width, height: content.height} ; } identity [FunctionDeclaration]: export function identity(): Mat2D{ return [1, 0, 0, 1, 0, 0] as Mat2D; } intersectRects [FunctionDeclaration]: export function intersectRects(left: Rect, right: Rect): Rect{ const x = Math.max(left.x, right.x); const y = Math.max(left.y, right.y); return{ x, y, width: Math.max(0, Math.min(left.x + left.width, right.x + right.width) - x), height: Math.max(0, Math.min(left.y + left.height, right.y + right.height) - y), } ; } invert [FunctionDeclaration]: export function invert(matrix: Mat2D): Mat2D{ const [a, b, c, d, e, f] = matrix; const det = a * d - b * c; if (det === 0) throw new Error('Mat2D.invert: non-invertible matrix (det = 0)'); const ia = d / det; const ib = -b / det; const ic = -c / det; const id = a / det; return [ia, ib, ic, id, -(ia * e + ic * f), -(ib * e + id * f)] as Mat2D; } isQuarterTurn [FunctionDeclaration]: export function isQuarterTurn(rotation: Rotation): boolean{ return rotation === 90 || rotation === 270; } matrixToCss [FunctionDeclaration]: export function matrixToCss(matrix: Mat2D): string{ const [a, b, c, d, e, f] = matrix; return `matrix(${ a} , ${ b} , ${ c} , ${ d} , ${ e} , ${ f} )`; } pageTransform [FunctionDeclaration]: export function pageTransform(input:{ pageSize: Size; rotation: Rotation; scale: number; baseScale?: number; dpr: number; } ): PageTransform{ const{ pageSize, rotation, scale, dpr} = input; const deviceWidth = Math.max(1, Math.round(pageSize.width * scale * dpr)); const deviceHeight = deviceHeightForWidth(pageSize, deviceWidth); const renderScale = deviceWidth / pageSize.width; const content: Size ={ width: deviceWidth / dpr, height: deviceHeight / dpr} ; const viewScale = content.width / pageSize.width; const baseScale = input.baseScale ?? scale; const zoom = baseScale > 0 ? viewScale / baseScale : 1; const footprint = displaySize(content, rotation); const contentMat = [viewScale, 0, 0, viewScale, 0, 0] as Mat2D; const viewMat = rotateScaleMatrix(viewScale, content.width, content.height, rotation); const invMat = invert(viewMat); const toPixels = (point: Point): Point => applyPoint(contentMat, point); const contentInv = invert(contentMat); const fromPixels = (point: Point): Point => applyPoint(contentInv, point); const pageToView = (point: Point): Point => applyPoint(viewMat, point); const viewToPage = (point: Point): Point => applyPoint(invMat, point); const pageToViewRect = (rect: Rect): Rect => applyRect(viewMat, rect); const viewToPageRect = (rect: Rect): Rect => applyRect(invMat, rect); const cssMatrix = matrixToCss(viewMat); return{ rotation, viewWidth: footprint.width, viewHeight: footprint.height, contentWidth: content.width, contentHeight: content.height, deviceWidth, deviceHeight, renderScale, viewScale, baseScale, zoom, toPixels, fromPixels, pageToView, pageToViewRect, viewToPage, viewToPageRect, cssMatrix, } ; } quadBounds [FunctionDeclaration]: export function quadBounds(quad: Quad): Rect{ const xs = [quad.upperLeft.x, quad.upperRight.x, quad.lowerLeft.x, quad.lowerRight.x]; const ys = [quad.upperLeft.y, quad.upperRight.y, quad.lowerLeft.y, quad.lowerRight.y]; const minX = Math.min(...xs); const minY = Math.min(...ys); return{ x: minX, y: minY, width: Math.max(...xs) - minX, height: Math.max(...ys) - minY} ; } quadCorners [FunctionDeclaration]: export function quadCorners(quad: Quad): [Point, Point, Point, Point]{ return [quad.upperLeft, quad.upperRight, quad.lowerLeft, quad.lowerRight]; } quadEdge [FunctionDeclaration]: export function quadEdge(quad: Quad, side: 'left' | 'right'): [Point, Point]{ return side === 'left' ? [quad.upperLeft, quad.lowerLeft] : [quad.upperRight, quad.lowerRight]; } quadEquals [FunctionDeclaration]: export function quadEquals(left: Quad, right: Quad): boolean{ const eq = (point: Point, other: Point) => point.x === other.x && point.y === other.y; return ( eq(left.upperLeft, right.upperLeft) && eq(left.upperRight, right.upperRight) && eq(left.lowerLeft, right.lowerLeft) && eq(left.lowerRight, right.lowerRight) ); } quadFromRect [FunctionDeclaration]: export function quadFromRect(rect: Rect): Quad{ return{ upperLeft:{ x: rect.x, y: rect.y} , upperRight:{ x: rect.x + rect.width, y: rect.y} , lowerLeft:{ x: rect.x, y: rect.y + rect.height} , lowerRight:{ x: rect.x + rect.width, y: rect.y + rect.height} , } ; } quadRing [FunctionDeclaration]: export function quadRing(quad: Quad): QuadRing{ return [quad.upperLeft, quad.upperRight, quad.lowerRight, quad.lowerLeft]; } rectContains [FunctionDeclaration]: export function rectContains(rect: Rect, point: Point): boolean{ return ( point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height ); } rectFromCorners [FunctionDeclaration]: export function rectFromCorners(from: Point, to: Point): Rect{ return{ x: Math.min(from.x, to.x), y: Math.min(from.y, to.y), width: Math.abs(to.x - from.x), height: Math.abs(to.y - from.y), } ; } rectsOverlap [FunctionDeclaration]: export function rectsOverlap(left: Rect, right: Rect): boolean{ const overlap = intersectRects(left, right); return overlap.width > 0 && overlap.height > 0; } rotate [FunctionDeclaration]: export function rotate(rad: number): Mat2D{ const cosine = Math.cos(rad); const sine = Math.sin(rad); return [cosine, sine, -sine, cosine, 0, 0] as Mat2D; } rotateAbout [FunctionDeclaration]: export function rotateAbout(point: PointIn, rad: number): Mat2D{ return compose( translate(point.x, point.y), compose(rotate(rad), translate(-point.x, -point.y)), ); } rotateScaleMatrix [FunctionDeclaration]: export function rotateScaleMatrix( scale: number, boxW: number, boxH: number, rotation: Rotation, ): Mat2D{ switch (rotation){ case 90: return [0, scale, -scale, 0, boxH, 0] as Mat2D; case 180: return [-scale, 0, 0, -scale, boxW, boxH] as Mat2D; case 270: return [0, -scale, scale, 0, 0, boxW] as Mat2D; default: return [scale, 0, 0, scale, 0, 0] as Mat2D; } } samePagePlacement [FunctionDeclaration]: export function samePagePlacement( left: Pick, right: Pick, ): boolean{ return ( left.viewScale === right.viewScale && left.zoom === right.zoom && left.rotation === right.rotation && left.contentWidth === right.contentWidth && left.contentHeight === right.contentHeight ); } scale [FunctionDeclaration]: export function scale(sx: number, sy: number): Mat2D{ return [sx, 0, 0, sy, 0, 0] as Mat2D; } scaleAbout [FunctionDeclaration]: export function scaleAbout( point: PointIn, sx: number, sy: number, ): Mat2D{ return compose( translate(point.x, point.y), compose(scale(sx, sy), translate(-point.x, -point.y)), ); } snapToDevice [FunctionDeclaration]: export function snapToDevice(value: number, dpr: number): number{ return Math.round(value * dpr) / dpr; } translate [FunctionDeclaration]: export function translate(tx: number, ty: number): Mat2D{ return [1, 0, 0, 1, tx, ty] as Mat2D; }