/* ============================================================
   units.jsx — pt ↔ px conversion helpers
   ------------------------------------------------------------
   The doc model stores `fontSize` in px (legacy). The UI shows
   pt (Word convention). 1pt = 4/3 px on standard 96dpi screens.
   ============================================================ */

const PT_PER_PX = 0.75;       // 1px = 0.75pt
const PX_PER_PT = 4 / 3;      // 1pt ≈ 1.3333px

// Round to nearest 0.5pt for clean display
function pxToPt(px) {
  return Math.round(px * PT_PER_PX * 2) / 2;
}
function ptToPx(pt) {
  return Math.round(pt * PX_PER_PT * 100) / 100;
}

// The pt scale we expose in the UI — Word-style
const PT_SCALE = [8, 9, 10, 11, 12, 13, 14, 16, 18];

// Snap a pt value to the nearest entry in PT_SCALE
function snapPt(pt) {
  let best = PT_SCALE[0];
  let bestDiff = Math.abs(pt - best);
  for (const v of PT_SCALE) {
    const d = Math.abs(pt - v);
    if (d < bestDiff) { best = v; bestDiff = d; }
  }
  return best;
}

// Format pt for display ("11pt", "10.5pt")
function fmtPt(pt) {
  if (Number.isInteger(pt)) return pt + 'pt';
  return pt.toFixed(1) + 'pt';
}

Object.assign(window, { pxToPt, ptToPx, PT_SCALE, snapPt, fmtPt });
