Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

10.8 KB
Raw Blame
// PDF point to real-world unit conversions

import type { TFunction } from "i18next";
import type {
  Measurement,
  MeasureScale,
  PagePoint,
  CalibrationMetadata,
} from "@editor/utils/measurementTypes";

// 1 PDF point in meters (1/72 inch)
const POINT_TO_METERS = 0.0254 / 72;

// Conversion factors: units per PDF point
export const POINT_TO_UNIT = {
  m: POINT_TO_METERS,
  cm: POINT_TO_METERS * 100,
  mm: POINT_TO_METERS * 1000,
  km: POINT_TO_METERS / 1000,
  ft: POINT_TO_METERS / 0.3048,
  in: POINT_TO_METERS / 0.0254,
  yd: POINT_TO_METERS / 0.9144,
  mi: POINT_TO_METERS / 1609.344,
} as const;

// Valid measurement units from POINT_TO_UNIT
export type MeasurementUnit = keyof typeof POINT_TO_UNIT;

function normalizeUnit(unit: string): string {
  return unit.toLowerCase().trim();
}

function isMeasurementUnit(unit: string): unit is MeasurementUnit {
  return Object.hasOwn(POINT_TO_UNIT, unit);
}

export function getUnitFactor(unit: string): number | undefined {
  const normalized = normalizeUnit(unit);
  if (!isMeasurementUnit(normalized)) {
    return undefined;
  }
  return POINT_TO_UNIT[normalized];
}

export function calculateScaleFactor(ratio: number, unit: string): number {
  if (!Number.isFinite(ratio) || ratio <= 0) {
    throw new Error(`Invalid scale ratio: ${ratio}`);
  }

  const normalized = normalizeUnit(unit);
  if (!isMeasurementUnit(normalized)) {
    throw new Error(`Unsupported unit: ${unit}`);
  }

  return POINT_TO_UNIT[normalized] * ratio;
}

export function generateScaleLabel(ratio: number | null, unit: string): string {
  if (ratio === null || ratio === undefined) {
    return unit;
  }
  const display = Number.isInteger(ratio)
    ? ratio.toString()
    : ratio.toFixed(2).replace(/\.?0+$/, "");
  return `1:${display} (${unit})`;
}

// Imperial units
const IMPERIAL_UNITS = ["ft", "in", "yd", "mi"] as const;
export function isImperialUnit(unit: string): boolean {
  const normalized = normalizeUnit(unit);
  return isMeasurementUnit(normalized)
    ? (IMPERIAL_UNITS as readonly MeasurementUnit[]).includes(normalized)
    : false;
}

export function convertUnit(
  value: number,
  sourceUnit: string,
  targetUnit: string,
): number | null {
  if (!Number.isFinite(value)) {
    return null;
  }

  const src = normalizeUnit(sourceUnit);
  const tgt = normalizeUnit(targetUnit);

  if (!isMeasurementUnit(src) || !isMeasurementUnit(tgt)) {
    return null;
  }

  const sourceFactor = POINT_TO_UNIT[src];
  const targetFactor = POINT_TO_UNIT[tgt];

  return value * (targetFactor / sourceFactor);
}

export function parsePresetRatio(preset: string): number | null {
  const parts = preset.split(":");

  // Must have exactly 2 parts and first part must be "1"
  if (parts.length !== 2 || parts[0].trim() !== "1") {
    return null;
  }

  const value = Number(parts[1]);
  return Number.isFinite(value) && value > 0 ? value : null;
}

// UI dropdown options - shared across components
export const UNIT_OPTIONS = [
  { value: "m", labelKey: "ruler.units.m", label: "Meters (m)" },
  { value: "cm", labelKey: "ruler.units.cm", label: "Centimeters (cm)" },
  { value: "mm", labelKey: "ruler.units.mm", label: "Millimeters (mm)" },
  { value: "km", labelKey: "ruler.units.km", label: "Kilometers (km)" },
  { value: "ft", labelKey: "ruler.units.ft", label: "Feet (ft)" },
  { value: "in", labelKey: "ruler.units.in", label: "Inches (in)" },
  { value: "yd", labelKey: "ruler.units.yd", label: "Yards (yd)" },
  { value: "mi", labelKey: "ruler.units.mi", label: "Miles (mi)" },
] as const;

/** UNIT_OPTIONS with labels translated; call at render time. */
export function getUnitOptions(
  t: TFunction,
): { value: (typeof UNIT_OPTIONS)[number]["value"]; label: string }[] {
  return UNIT_OPTIONS.map(({ value, labelKey, label }) => ({
    value,
    label: t(labelKey, label),
  }));
}

const MAX_SESSION_ENTRIES = 50;
const TRIMMED_SESSION_ENTRIES = 40;

/**
 * Detect quota exceeded errors across browser implementations.
 * Handles: name "QuotaExceededError", code 22 (legacy), "NS_ERROR_DOM_QUOTA_REACHED"
 *
 * Note: DOMException may not be instanceof Error in all browsers,
 * so we check by shape and properties rather than type.
 * Note: DOMException.code is deprecated but kept for legacy browser support.
 */
function isQuotaExceededError(error: unknown): boolean {
  if (error === null || error === undefined) return false;

  // Check if it's a DOMException when available (standard)
  if (typeof DOMException !== "undefined" && error instanceof DOMException) {
    if (error.name === "QuotaExceededError") return true;
  }

  // Fallback: check by shape for any object with name/code properties
  if (typeof error === "object") {
    const err = error as Record<string, unknown>;

    // Modern standard: check name property (works in all modern browsers)
    if (err.name === "QuotaExceededError") return true;
    if (err.name === "NS_ERROR_DOM_QUOTA_REACHED") return true;

    // Legacy support: check deprecated code property for very old browsers
    // Use Object.hasOwn for safe own-property check
    if (Object.hasOwn(err, "code") && err.code === 22) return true;
  }

  return false;
}

// Load entries from sessionStorage
export function loadSessionMap(key: string): Record<string, unknown> {
  try {
    const raw = sessionStorage.getItem(key);
    if (!raw) return {};

    const data = JSON.parse(raw);
    if (typeof data !== "object" || data === null || Array.isArray(data)) {
      return {};
    }

    return data as Record<string, unknown>;
  } catch {
    // Silently return empty object on parse error
    try {
      sessionStorage.removeItem(key);
    } catch {
      // Ignore cleanup errors
    }
    return {};
  }
}

// Save entry to sessionStorage with quota management.
export function saveSessionMap(
  key: string,
  fileKey: string,
  value: MeasureScale | Measurement[] | null,
): void {
  if (!fileKey) return;

  try {
    const existing: Record<string, unknown> = {
      ...loadSessionMap(key),
    };

    // Delete first to move fileKey to end (maintains insertion order recency)
    delete existing[fileKey];
    existing[fileKey] = value;

    // Trim back below the max to avoid pruning again on every subsequent save.
    const keys = Object.keys(existing);
    if (keys.length > MAX_SESSION_ENTRIES) {
      const entriesToDelete = keys.slice(
        0,
        keys.length - TRIMMED_SESSION_ENTRIES,
      );
      entriesToDelete.forEach((k) => delete existing[k]);
    }

    sessionStorage.setItem(key, JSON.stringify(existing));
  } catch (e) {
    // Quota exceeded - try clearing and retrying (handles cross-browser error variants)
    if (isQuotaExceededError(e)) {
      try {
        sessionStorage.removeItem(key);
        // Retry with fresh storage
        const fresh: Record<string, unknown> = { [fileKey]: value };
        sessionStorage.setItem(key, JSON.stringify(fresh));
      } catch {
        // Silently ignore if retry fails - data loss is acceptable
      }
    }
    // Silently ignore other storage errors
  }
}

// Validation helpers

export function validatePagePoint(obj: unknown): obj is PagePoint {
  if (typeof obj !== "object" || obj === null) return false;

  const pt = obj as Record<string, unknown>;
  return (
    typeof pt.pageIndex === "number" &&
    Number.isFinite(pt.pageIndex) &&
    pt.pageIndex >= 0 &&
    typeof pt.x === "number" &&
    Number.isFinite(pt.x) &&
    typeof pt.y === "number" &&
    Number.isFinite(pt.y)
  );
}

// MeasureScale can be null (reset) or valid object
export function validateMeasureScale(obj: unknown): obj is MeasureScale | null {
  // null is allowed (reset to default)
  if (obj === null) return true;

  if (typeof obj !== "object") return false;

  const s = obj as Record<string, unknown>;

  // Validate factor: must be positive finite number
  if (
    typeof s.factor !== "number" ||
    !Number.isFinite(s.factor) ||
    s.factor <= 0
  ) {
    return false;
  }

  // Validate ratio: optional, but if present must be positive finite number
  if (
    s.ratio !== null &&
    (typeof s.ratio !== "number" || !Number.isFinite(s.ratio) || s.ratio <= 0)
  ) {
    return false;
  }

  // Validate unit: must be non-empty string and exist in POINT_TO_UNIT
  if (typeof s.unit !== "string" || s.unit.trim().length === 0) {
    return false;
  }

  const normalized = normalizeUnit(s.unit);
  if (!isMeasurementUnit(normalized)) {
    return false;
  }

  return true;
}

// Reject cross-page measurements
export function validateMeasurement(obj: unknown): obj is Measurement {
  if (typeof obj !== "object" || obj === null) return false;

  const m = obj as Record<string, unknown>;

  // Validate structure
  if (
    !(
      typeof m.id === "string" &&
      m.id.trim().length > 0 &&
      validatePagePoint(m.start) &&
      validatePagePoint(m.end)
    )
  ) {
    return false;
  }

  // Reject cross-page measurements
  const start = m.start;
  const end = m.end;
  if (start.pageIndex !== end.pageIndex) {
    return false;
  }

  return true;
}

export function formatPaperDistance(distancePts: number): string {
  if (!Number.isFinite(distancePts) || distancePts < 0) {
    return "0 mm";
  }

  const inches = distancePts / 72;
  const mm = inches * 25.4;

  if (mm < 100) {
    return `${mm.toFixed(1)} mm`;
  }
  if (mm < 1000) {
    return `${(mm / 10).toFixed(1)} cm`;
  }
  return `${(mm / 1000).toFixed(2)} m`;
}

export function validateRealDistance(value: unknown): number | null {
  if (value === null || value === undefined || value === "") {
    return null;
  }

  const num = typeof value === "number" ? value : Number(value);

  if (!Number.isFinite(num) || num <= 0) {
    return null;
  }

  return num;
}

export function deriveRatioFromFactor(
  factor: number,
  unit: string,
): number | null {
  if (!Number.isFinite(factor) || factor <= 0) {
    return null;
  }

  const baseFactor = getUnitFactor(unit);
  if (!baseFactor) {
    return null;
  }

  // ratio = factor / baseFactor
  const ratio = factor / baseFactor;
  return Number.isFinite(ratio) && ratio > 0 ? ratio : null;
}

export function calculateCalibratedScale(
  pdfDistancePts: number,
  realDistance: number,
  unit: string,
): MeasureScale {
  if (!Number.isFinite(pdfDistancePts) || pdfDistancePts <= 0) {
    throw new Error("Invalid PDF distance (must be positive)");
  }

  if (!Number.isFinite(realDistance) || realDistance <= 0) {
    throw new Error("Invalid real-world distance (must be positive)");
  }

  const baseFactor = getUnitFactor(unit);
  if (!baseFactor) {
    throw new Error(`Unsupported unit: ${unit}`);
  }

  const factor = realDistance / pdfDistancePts;
  const ratio = deriveRatioFromFactor(factor, unit);

  return {
    factor,
    ratio,
    unit,
  };
}

export function createCalibrationMetadata(
  pdfDistancePts: number,
  realDistance: number,
  scale: MeasureScale,
  unitUsed: string,
): CalibrationMetadata {
  return {
    pdfDistancePts,
    realDistance,
    scale,
    timestamp: new Date().toISOString(),
    unitUsed,
  };
}