/** * Geometry primitives - Point, Rect, IRect, Matrix, Quad * * This module provides fundamental 2D geometry types used throughout the MicroPDF library. * All types are immutable and follow functional programming principles. * * This implementation mirrors the Rust `fitz::geometry` module for 100% API compatibility. * * @module geometry * @example * ```typescript * import { Point, Rect, Matrix } from 'micropdf'; * * // Create a point * const p = new Point(100, 200); * * // Create a rectangle * const rect = new Rect(0, 0, 100, 100); * * // Transform with a matrix * const matrix = Matrix.scale(2, 2); * const transformed = p.transform(matrix); * ``` */ import type { PointLike, RectLike, IRectLike, MatrixLike, QuadLike } from './types.js'; export type { PointLike, RectLike, IRectLike, MatrixLike, QuadLike }; /** * A 2D point with floating-point coordinates. * * Points are immutable - all operations return new Point instances rather than * modifying the existing point. This makes them safe to use in functional programming * contexts and prevents accidental mutations. * * @class Point * @implements {PointLike} * @example * ```typescript * // Create a point * const p1 = new Point(10, 20); * * // Transform it * const p2 = p1.scale(2); // Point(20, 40) * * // Calculate distance * const distance = p1.distanceTo(p2); // 22.36... * * // Points are immutable * const p3 = p1.add(new Point(5, 5)); // p1 is unchanged * ``` */ export declare class Point implements PointLike { /** * The x-coordinate of the point. * @readonly * @type {number} */ readonly x: number; /** * The y-coordinate of the point. * @readonly * @type {number} */ readonly y: number; /** * Creates a new Point with the specified coordinates. * * @param {number} x - The x-coordinate * @param {number} y - The y-coordinate * @example * ```typescript * const point = new Point(100, 200); * console.log(point.x); // 100 * console.log(point.y); // 200 * ``` */ constructor(x: number, y: number); /** * The origin point at coordinates (0, 0). * * This is a convenience constant for the most commonly used point. * * @static * @readonly * @type {Point} * @example * ```typescript * const origin = Point.ORIGIN; * console.log(origin.x, origin.y); // 0, 0 * ``` */ static readonly ORIGIN: Point; /** * Returns the origin point (0, 0). * * Alias for {@link Point.ORIGIN}. * * @static * @returns {Point} The origin point */ static zero(): Point; /** * Creates a Point from a point-like object. * * This method accepts any object with `x` and `y` properties and converts it * to a proper Point instance. If the input is already a Point, it returns it unchanged. * * @static * @param {PointLike} p - A point-like object with x and y properties * @returns {Point} A Point instance * @example * ```typescript * // From a plain object * const p1 = Point.from({ x: 10, y: 20 }); * * // From an existing Point (returns the same instance) * const p2 = new Point(10, 20); * const p3 = Point.from(p2); // p2 === p3 * ``` */ static from(p: PointLike): Point; /** * Transforms this point by a transformation matrix. * * Applies a 2D affine transformation to the point. This is commonly used for * scaling, rotation, translation, and skewing operations. * * @param {MatrixLike} m - The transformation matrix to apply * @returns {Point} A new transformed point * @example * ```typescript * const p = new Point(10, 20); * * // Scale by 2x * const scaled = p.transform(Matrix.scale(2, 2)); * console.log(scaled); // Point(20, 40) * * // Rotate 90 degrees * const rotated = p.transform(Matrix.rotate(Math.PI / 2)); * ``` */ transform(m: MatrixLike): Point; /** * Calculates the Euclidean distance to another point. * * Uses the Pythagorean theorem to compute the straight-line distance * between this point and another point in 2D space. * * @param {PointLike} other - The point to measure distance to * @returns {number} The distance in the same units as the coordinates * @example * ```typescript * const p1 = new Point(0, 0); * const p2 = new Point(3, 4); * const distance = p1.distanceTo(p2); // 5.0 * * // Distance is symmetric * p2.distanceTo(p1) === p1.distanceTo(p2); // true * ``` */ distanceTo(other: PointLike): number; /** * Adds another point to this point (vector addition). * * Returns a new point whose coordinates are the sum of this point's * coordinates and the other point's coordinates. * * @param {PointLike} other - The point to add * @returns {Point} A new point with summed coordinates * @example * ```typescript * const p1 = new Point(10, 20); * const p2 = new Point(5, 10); * const sum = p1.add(p2); * console.log(sum); // Point(15, 30) * ``` */ add(other: PointLike): Point; /** * Subtracts another point from this point (vector subtraction). * * Returns a new point whose coordinates are the difference between * this point's coordinates and the other point's coordinates. * * @param {PointLike} other - The point to subtract * @returns {Point} A new point with the difference * @example * ```typescript * const p1 = new Point(10, 20); * const p2 = new Point(5, 10); * const diff = p1.subtract(p2); * console.log(diff); // Point(5, 10) * ``` */ subtract(other: PointLike): Point; /** * Scales this point by a factor (scalar multiplication). * * Multiplies both x and y coordinates by the given factor. * * @param {number} factor - The scaling factor * @returns {Point} A new scaled point * @example * ```typescript * const p = new Point(10, 20); * const doubled = p.scale(2); // Point(20, 40) * const halved = p.scale(0.5); // Point(5, 10) * const negated = p.scale(-1); // Point(-10, -20) * ``` */ scale(factor: number): Point; /** * Normalizes this point to unit length. * * Returns a point in the same direction but with length 1.0. * If the point is at the origin (length 0), returns the origin. * * @returns {Point} A normalized point with length 1.0 (or origin if length is 0) * @example * ```typescript * const p = new Point(3, 4); * const normalized = p.normalize(); * console.log(normalized); // Point(0.6, 0.8) * console.log(normalized.length); // 1.0 * * // Origin stays at origin * Point.ORIGIN.normalize(); // Point(0, 0) * ``` */ normalize(): Point; /** * Gets the length (magnitude) of this point when treated as a vector. * * Computes the Euclidean distance from the origin to this point. * * @readonly * @type {number} * @example * ```typescript * const p = new Point(3, 4); * console.log(p.length); // 5.0 * * // Right triangle: 3² + 4² = 5² * ``` */ get length(): number; /** * Checks if this point is equal to another point. * * Two points are considered equal if both their x and y coordinates are exactly equal. * Note: This uses strict equality, so floating point precision issues may affect the result. * * @param {PointLike} other - The point to compare with * @returns {boolean} True if the points have identical coordinates * @example * ```typescript * const p1 = new Point(10, 20); * const p2 = new Point(10, 20); * const p3 = new Point(10, 21); * * p1.equals(p2); // true * p1.equals(p3); // false * ``` */ equals(other: PointLike): boolean; /** * Calculates the Euclidean distance to another point. * * Alias for {@link Point.distanceTo}. * * @param {PointLike} other - The point to measure distance to * @returns {number} The distance */ distance(other: PointLike): number; /** * Creates a copy of this point. * * @returns {Point} A new Point with the same coordinates */ clone(): Point; /** * Returns a string representation of the point. * * @returns {string} A string in the format "Point(x, y)" * @example * ```typescript * const p = new Point(10.5, 20.3); * console.log(p.toString()); // "Point(10.5, 20.3)" * console.log(String(p)); // "Point(10.5, 20.3)" * ``` */ toString(): string; } /** * A rectangle defined by two corner points (floating point) */ export declare class Rect implements RectLike { readonly x0: number; readonly y0: number; readonly x1: number; readonly y1: number; constructor(x0: number, y0: number, x1: number, y1: number); /** Empty rectangle */ static readonly EMPTY: Rect; /** Infinite rectangle */ static readonly INFINITE: Rect; /** Unit rectangle (0,0) to (1,1) */ static readonly UNIT: Rect; /** * Returns an empty rectangle. * * Alias for {@link Rect.EMPTY}. * * @static * @returns {Rect} An empty rectangle */ static empty(): Rect; /** * Returns an infinite rectangle. * * Alias for {@link Rect.INFINITE}. * * @static * @returns {Rect} An infinite rectangle */ static infinite(): Rect; /** Create a rect from a rect-like object */ static from(r: RectLike): Rect; /** Create a rect from position and size */ static fromXYWH(x: number, y: number, width: number, height: number): Rect; /** Create a rect from an IRect */ static fromIRect(r: IRectLike): Rect; /** Width of the rectangle */ get width(): number; /** Height of the rectangle */ get height(): number; /** Check if the rectangle is empty */ isEmpty(): boolean; /** Check if the rectangle is infinite */ isInfinite(): boolean; /** Check if a point is inside the rectangle */ containsPoint(p: PointLike): boolean; containsPoint(x: number, y: number): boolean; /** Check if this rectangle contains another rectangle */ containsRect(other: RectLike): boolean; /** Union with another rectangle */ union(other: RectLike): Rect; /** Intersection with another rectangle */ intersect(other: RectLike): Rect; /** Expand by including a point */ includePoint(p: PointLike): Rect; /** Translate by offset */ translate(dx: number, dy: number): Rect; /** Scale by factor */ scale(sx: number, sy?: number): Rect; /** Transform by a matrix */ transform(m: MatrixLike): Rect; /** Normalize (ensure x0 <= x1 and y0 <= y1) */ normalize(): Rect; /** Round to integer rectangle */ round(): IRect; /** * Check if a point is inside the rectangle. * * Alias for {@link Rect.containsPoint}. * * @param {PointLike} point - The point to test * @returns {boolean} True if the point is inside */ contains(point: PointLike): boolean; /** * Expand (or shrink) the rectangle by a value in all directions. * * A positive value grows the rectangle; a negative value shrinks it. * * @param {number} value - The amount to expand each edge by * @returns {Rect} A new expanded rectangle */ expand(value: number): Rect; /** * Creates a copy of this rectangle. * * @returns {Rect} A new Rect with the same coordinates */ clone(): Rect; /** * Returns the center point of the rectangle. * * @returns {Point} The center point */ center(): Point; /** Check equality */ equals(other: RectLike): boolean; toString(): string; } /** * An integer rectangle defined by two corner points */ export declare class IRect implements IRectLike { readonly x0: number; readonly y0: number; readonly x1: number; readonly y1: number; constructor(x0: number, y0: number, x1: number, y1: number); /** Empty integer rectangle */ static readonly EMPTY: IRect; /** Infinite integer rectangle */ static readonly INFINITE: IRect; /** Create from a rect-like object */ static from(r: IRectLike): IRect; /** Create from a Rect by rounding */ static fromRect(r: RectLike): IRect; /** * Create from a Rect by rounding each coordinate to the nearest integer. * * Unlike {@link IRect.fromRect} which floors origins and ceils extents, * this method uses standard rounding for all four coordinates. * * @param {RectLike} r - The source rectangle * @returns {IRect} A new integer rectangle with rounded coordinates */ static round(r: RectLike): IRect; /** Width */ get width(): number; /** Height */ get height(): number; /** Check if empty */ isEmpty(): boolean; /** Union with another integer rectangle */ union(other: IRectLike): IRect; /** Intersection with another integer rectangle */ intersect(other: IRectLike): IRect; /** Translate by offset */ translate(dx: number, dy: number): IRect; /** Convert to Rect */ toRect(): Rect; /** * Check if a point is inside the integer rectangle. * * @param {PointLike} point - The point to test * @returns {boolean} True if the point is inside */ contains(point: PointLike): boolean; /** Check equality */ equals(other: IRectLike): boolean; toString(): string; } /** * A 2D transformation matrix (affine transform) */ export declare class Matrix implements MatrixLike { readonly a: number; readonly b: number; readonly c: number; readonly d: number; readonly e: number; readonly f: number; constructor(a: number, b: number, c: number, d: number, e: number, f: number); /** Identity matrix */ static readonly IDENTITY: Matrix; /** * Returns the identity matrix. * * Alias for {@link Matrix.IDENTITY}. * * @static * @returns {Matrix} The identity matrix */ static identity(): Matrix; /** Create a matrix from a matrix-like object */ static from(m: MatrixLike): Matrix; /** Create a translation matrix */ static translate(tx: number, ty: number): Matrix; /** Create a scaling matrix */ static scale(sx: number, sy?: number): Matrix; /** Create a rotation matrix (radians) */ static rotate(radians: number): Matrix; /** Create a shear matrix */ static shear(sx: number, sy: number): Matrix; /** Check if this is the identity matrix */ isIdentity(): boolean; /** Check if this is a rectilinear matrix (no rotation/shear) */ isRectilinear(): boolean; /** Concatenate with another matrix */ concat(other: MatrixLike): Matrix; /** Invert the matrix */ invert(): Matrix | null; /** Pre-translate this matrix */ preTranslate(tx: number, ty: number): Matrix; /** Post-translate this matrix */ postTranslate(tx: number, ty: number): Matrix; /** Pre-scale this matrix */ preScale(sx: number, sy?: number): Matrix; /** Post-scale this matrix */ postScale(sx: number, sy?: number): Matrix; /** Pre-rotate this matrix */ preRotate(radians: number): Matrix; /** Post-rotate this matrix */ postRotate(radians: number): Matrix; /** Pre-shear this matrix */ preShear(sx: number, sy: number): Matrix; /** Post-shear this matrix */ postShear(sx: number, sy: number): Matrix; /** Transform a point */ transformPoint(p: PointLike): Point; /** * Transform a rectangle using this matrix. * * Transforms all four corners of the rectangle and returns the * axis-aligned bounding box of the result. * * @param {RectLike} r - The rectangle to transform * @returns {Rect} The transformed bounding rectangle */ transformRect(r: RectLike): Rect; /** * Multiply this matrix with another matrix. * * Alias for {@link Matrix.concat}. * * @param {MatrixLike} other - The matrix to multiply with * @returns {Matrix} The product matrix */ multiply(other: MatrixLike): Matrix; /** * Creates a copy of this matrix. * * @returns {Matrix} A new Matrix with the same values */ clone(): Matrix; /** * Calculates the determinant of this matrix. * * @returns {number} The determinant (ad - bc) */ determinant(): number; /** * Extracts the scale factors from this matrix. * * Returns a Point where x is the horizontal scale and y is the * vertical scale, computed as the lengths of the basis vectors. * * @returns {Point} A point with (scaleX, scaleY) */ getScale(): Point; /** Check equality */ equals(other: MatrixLike): boolean; toString(): string; } /** * A quadrilateral defined by four corner points */ export declare class Quad implements QuadLike { readonly ul: Point; readonly ur: Point; readonly ll: Point; readonly lr: Point; constructor(ul: PointLike, ur: PointLike, ll: PointLike, lr: PointLike); /** Create a quad from a rectangle */ static fromRect(r: RectLike): Quad; /** Transform this quad by a matrix */ transform(m: MatrixLike): Quad; /** Get the bounding rectangle */ get bounds(): Rect; /** Check if a point is inside the quad */ containsPoint(p: PointLike): boolean; /** Check if this is a valid quad (non-self-intersecting) */ get isValid(): boolean; /** * Get the bounding box of this quad. * * Alias for the {@link Quad.bounds} property. * * @returns {Rect} The axis-aligned bounding rectangle */ bbox(): Rect; /** * Check if this quad is convex (non-self-intersecting). * * Alias for the {@link Quad.isValid} property. * * @returns {boolean} True if the quad is convex */ isConvex(): boolean; /** * Check if this quad is rectilinear (all edges are axis-aligned). * * A rectilinear quad has all edges parallel to the x or y axis. * * @returns {boolean} True if all edges are axis-aligned */ isRectilinear(): boolean; /** * Creates a copy of this quad. * * @returns {Quad} A new Quad with the same corner points */ clone(): Quad; toString(): string; } /** * Color-like type */ export type ColorLike = Color | { r: number; g: number; b: number; a?: number; } | [number, number, number] | [number, number, number, number]; /** * An RGBA color */ export declare class Color { readonly r: number; readonly g: number; readonly b: number; readonly a: number; constructor(r: number, g: number, b: number, a?: number); /** * Create color from color-like object */ static from(c: ColorLike): Color; /** Black color */ static readonly BLACK: Color; /** White color */ static readonly WHITE: Color; /** Red color */ static readonly RED: Color; /** Green color */ static readonly GREEN: Color; /** Blue color */ static readonly BLUE: Color; /** * Get as array [r, g, b, a] */ toArray(): [number, number, number, number]; } //# sourceMappingURL=geometry.d.ts.map