import L from '@edgepdf/core'; import type { ImageInfo, ViewerCoords, ImageCoords, CoordinateBounds } from '@edgepdf/types'; /** * Image position information within the tile grid canvas */ export interface ImagePosition { /** Width of rendered image in canvas coordinates */ imageRenderWidth: number; /** Height of rendered image in canvas coordinates */ imageRenderHeight: number; /** X offset of image within canvas */ imageOffsetX: number; /** Y offset of image within canvas */ imageOffsetY: number; /** Total canvas width */ canvasWidth: number; /** Total canvas height */ canvasHeight: number; } /** * CoordinateMapper - Handles coordinate conversion between Leaflet and image pixel coordinates * * This class provides bidirectional coordinate conversion between: * - Leaflet coordinates [y, x] (using custom CRS) * - Image pixel coordinates {x, y} * * The conversion accounts for aspect ratio differences between the image and the tile grid canvas, * properly handling cases where the image doesn't fill the entire canvas. * * @example * ```typescript * const mapper = new CoordinateMapper({ * width: 2000, * height: 3000, * tileSize: 256, * maxZoom: 5, * minZoom: 0 * }); * * // Convert Leaflet coordinates to image coordinates * const imageCoords = mapper.leafletToImage([1500, 1000]); * // { x: 1000, y: 1500, withinBounds: true } * * // Convert image coordinates to Leaflet coordinates * const leafletCoords = mapper.imageToLeaflet({ x: 1000, y: 1500 }); * // [1500, 1000] * ``` */ export declare class CoordinateMapper { private imageInfo; /** * Creates a new CoordinateMapper instance * * @param imageInfo - Image information including width, height, and zoom levels * * @throws {Error} If imageInfo is invalid */ constructor(imageInfo: ImageInfo); /** * Calculates the image position and dimensions within the tile grid canvas. * This handles aspect ratio differences between the image and the canvas. * * Backward compatibility: Supports both legacy doubled grid (minZoom=0) and * standard grid (minZoom>=1) based on the minZoom value. * * @returns Object containing image render dimensions and offsets */ calculateImagePosition(): ImagePosition; /** * Converts Leaflet coordinates to image pixel coordinates. * * Accounts for image position within the tile grid canvas and aspect ratio differences. * * @param leafletCoords - Leaflet coordinates [y, x] (lat, lng in CRS.Simple) * @returns Object containing image pixel coordinates and bounds check * * @example * ```typescript * const result = mapper.leafletToImage([1500, 1000]); * // { x: 1000, y: 1500, withinBounds: true } * ``` */ leafletToImage(leafletCoords: ViewerCoords): ImageCoords & { withinBounds: boolean; }; /** * Converts image pixel coordinates to Leaflet coordinates. * * Accounts for image position within the tile grid canvas and aspect ratio differences. * * @param imageCoords - Image pixel coordinates {x, y} * @returns Leaflet coordinates [y, x] (lat, lng in CRS.Simple) * * @example * ```typescript * const leafletCoords = mapper.imageToLeaflet({ x: 1000, y: 1500 }); * // [1500, 1000] * ``` */ imageToLeaflet(imageCoords: ImageCoords): ViewerCoords; /** * Calculates the center point of the image in Leaflet coordinates. * * @returns Object containing center coordinates {x, y} in Leaflet coordinate system * * @example * ```typescript * const center = mapper.getImageCenter(); * // { x: 1000, y: 1500 } - center of a 2000x3000 image * ``` */ getImageCenter(): { x: number; y: number; }; /** * Calculates the bounds for the map based on the tile grid. * * Uses the minimum zoom level to ensure consistent coordinate system * across all zoom levels. * * Backward compatibility: Supports both legacy doubled grid (minZoom=0) and * standard grid (minZoom>=1) based on the minZoom value. * * @returns Leaflet bounds expression [[minY, minX], [maxY, maxX]] * * @example * ```typescript * const bounds = mapper.calculateMapBounds(); * // Legacy (minZoom=0): [[0, 0], [512, 512]] (2^(0+1) * 256) * // Standard (minZoom=1): [[0, 0], [512, 512]] (2^1 * 256) * ``` */ calculateMapBounds(): L.LatLngBoundsExpression; /** * Validates Leaflet coordinates and checks bounds * * @param coords - Leaflet coordinates [y, x] * @throws {Error} If coordinates are invalid or out of bounds */ validateLeafletCoords(coords: ViewerCoords): void; /** * Validates image pixel coordinates and checks bounds * * @param coords - Image pixel coordinates {x, y} * @throws {Error} If coordinates are invalid or out of bounds */ validateImageCoords(coords: ImageCoords): void; /** * Creates a custom CRS (Coordinate Reference System) for direct tile coordinate mapping. * This extends Leaflet's Simple CRS with a custom transformation. * * @returns Custom CRS instance */ createCustomCRS(): L.CRS; /** * Calculates the aspect ratio of the image * * @returns Aspect ratio (width / height) * * @example * ```typescript * const aspectRatio = mapper.getAspectRatio(); * // 0.666... for a 2000x3000 image * ``` */ getAspectRatio(): number; /** * Gets the coordinate bounds for the image * * @returns Coordinate bounds object * * @example * ```typescript * const bounds = mapper.getBounds(); * // { minX: 0, maxX: 2000, minY: 0, maxY: 3000 } * ``` */ getBounds(): CoordinateBounds; /** * Gets the image information * * @returns Image info object */ getImageInfo(): ImageInfo; /** * Checks if coordinates are within bounds * * @param coords - Image pixel coordinates {x, y} * @returns True if coordinates are within bounds, false otherwise * * @example * ```typescript * const isValid = mapper.isWithinBounds({ x: 1000, y: 1500 }); * // true * ``` */ isWithinBounds(coords: ImageCoords): boolean; /** * Clamps coordinates to valid bounds * * If coordinates are outside bounds, they are clamped to the nearest valid value. * * @param coords - Image pixel coordinates {x, y} * @returns Clamped coordinates within bounds * * @example * ```typescript * const clamped = mapper.clampToBounds({ x: 2500, y: 3500 }); * // { x: 2000, y: 3000 } (clamped to max bounds) * ``` */ clampToBounds(coords: ImageCoords): ImageCoords; /** * Calculates offset for centering coordinates * * This is useful for centering a point in a viewport or container. * * @param coords - Image pixel coordinates {x, y} to center * @param containerWidth - Width of the container/viewport * @param containerHeight - Height of the container/viewport * @returns Offset coordinates {x, y} to apply for centering * * @example * ```typescript * const offset = mapper.calculateCenterOffset( * { x: 1000, y: 1500 }, * 800, // container width * 600 // container height * ); * // { x: 600, y: 1200 } - offset to center the point * ``` */ calculateCenterOffset(coords: ImageCoords, containerWidth: number, containerHeight: number): ImageCoords; } //# sourceMappingURL=coordinate-mapper.d.ts.map