{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "focus-front-swap",
  "title": "Focus Front Swap",
  "description": "A headline pulls focus front-to-back to swap its last word to \"tonight\".",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "registryDependencies": ["@remocn/gsh"],
  "files": [
    {
      "path": "registry/remocn/focus-front-swap/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont as loadSans } from \"@remotion/google-fonts/Geist\";\nimport { loadFont as loadManrope } from \"@remotion/google-fonts/Manrope\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  spring,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\nimport type { GradientParams } from \"@/lib/remocn/gsh/gradient-engine\";\nimport { GshGround, gshParams } from \"@/lib/remocn/gsh/gsh-ground\";\nimport type { GshPresetId } from \"@/lib/remocn/gsh/presets\";\n\nconst sans = loadSans(\"normal\", {\n  weights: [\"300\", \"400\"],\n  subsets: [\"latin\"],\n});\nconst manrope = loadManrope(\"normal\", {\n  weights: [\"600\"],\n  subsets: [\"latin\"],\n});\n\nconst REMOCN_R_PATH =\n  \"M 0.01 0.81 C 0.01 1.73, 0.36 2.79, 1.09 4.13 C 4.91 11.04, 13.45 17.16, 21.7 18.9 C 22.94 19.16, 23.18 19.16, 51.39 19.27 C 76.07 19.36, 80.02 19.4, 81.32 19.57 C 89.89 20.69, 96.2 24.68, 99.38 31.01 C 103.19 38.56, 102.53 50.1, 97.91 57.07 C 94.66 61.96, 89.68 64.99, 83.26 66 C 82.81 66.07, 70.18 66.83, 55.2 67.69 C 24.82 69.43, 27.03 69.23, 24.18 70.4 C 19.9 72.15, 14.84 75.7, 10.65 79.89 C 4.86 85.68, 1.3 91.91, 0.25 98.13 C 0.12 98.85, 0.08 103.13, 0.04 116.66 L 0 134.26 9.5 134.26 L 19 134.26 19.05 119.41 C 19.1 103.27, 19.08 103.82, 19.82 101.04 C 21.79 93.65, 27.86 88.75, 36.45 87.63 C 37.23 87.53, 39.41 87.5, 43.57 87.53 C 50.12 87.59, 50.75 87.65, 53.22 88.45 C 56.61 89.56, 59.67 91.86, 62.02 95.07 C 62.52 95.76, 69.35 103.85, 77.2 113.07 C 85.04 122.28, 91.63 130.04, 91.85 130.32 C 92.07 130.59, 92.5 131.34, 92.82 131.97 C 93.52 133.35, 94.11 133.93, 95.06 134.13 C 95.5 134.23, 98.97 134.26, 106.36 134.23 L 117.01 134.19 100.82 113.07 C 91.91 101.45, 84.52 91.78, 84.39 91.57 C 83.36 89.89, 83.66 87.53, 85.09 86 C 85.79 85.25, 86.36 84.94, 88.07 84.38 C 96.18 81.72, 104.15 76.62, 109.97 70.36 C 120.59 58.93, 124.06 44.32, 119.44 30.43 C 114.59 15.81, 101.93 4.02, 87.64 0.83 C 83.93 0.01, 88.09 0.08, 41.85 0.04 L 0.01 0 0.01 0.81 Z\";\n\nfunction RemocnLogo({ fontSize, color }: { fontSize: number; color: string }) {\n  return (\n    <div\n      style={{\n        display: \"flex\",\n        alignItems: \"baseline\",\n        color,\n        fontFamily: manrope.fontFamily,\n        fontWeight: 600,\n        fontSize,\n        letterSpacing: \"-0.025em\",\n        lineHeight: 1,\n      }}\n    >\n      <svg\n        viewBox=\"0 0 124.06 134.26\"\n        style={{ height: \"0.76em\", width: \"auto\" }}\n        aria-hidden\n      >\n        <path fill=\"currentColor\" d={REMOCN_R_PATH} />\n      </svg>\n      <span style={{ marginLeft: \"0.04em\" }}>emocn</span>\n    </div>\n  );\n}\n\nexport interface FocusFrontSwapProps {\n  lines?: string[];\n  accent?: string;\n  ink?: string;\n  logo?: boolean;\n  theme?: \"dark\" | \"light\";\n  ground?: GshPresetId;\n  groundParams?: Partial<GradientParams>;\n  swap?: number;\n  blur?: number;\n}\n\nconst THEMES = {\n  dark: {\n    ink: \"#eef3ff\",\n    accent: \"#7fb0ff\",\n    logo: \"#eef3ff\",\n    ground: \"film-blue\" as GshPresetId | undefined,\n    groundParams: undefined as Partial<GradientParams> | undefined,\n    groundOffset: 5,\n    veil: \"2,5,12\",\n    veilIn: 0.6,\n    veilOut: 0.08,\n  },\n  light: {\n    ink: \"#0e1430\",\n    accent: \"#2f4dff\",\n    logo: \"#0e1430\",\n    ground: \"paper-crease\" as GshPresetId | undefined,\n    groundParams: undefined as Partial<GradientParams> | undefined,\n    groundOffset: 4,\n    veil: \"238,240,246\",\n    veilIn: 0.6,\n    veilOut: 0.04,\n  },\n};\n\nconst BEAT = {\n  enter: 18,\n  swap: 30,\n  minSwap: 18,\n  decay: 0.8,\n  logo: -3,\n};\n\nconst FONT_PX = 200;\nconst TRACK = -0.032;\nconst SOFT = 0.36;\nconst FRONT = Easing.bezier(0.65, 0, 0.35, 1);\nconst GLYPH_IN = Easing.bezier(0.22, 1, 0.36, 1);\nconst GLYPH_OUT = Easing.bezier(0.55, 0, 1, 0.45);\nconst CHIP = { mass: 0.5, damping: 12, stiffness: 200 };\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\ninterface Measure {\n  w: (s: string) => number;\n  asc: number;\n  desc: number;\n}\n\nfunction buildMeasure(_version: number): Measure {\n  const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n  const font = `300 ${FONT_PX}px \"${sans.fontFamily}\"`;\n  const cache: Record<string, number> = {};\n  let asc = 0.95;\n  let desc = 0.25;\n  if (ctx) {\n    ctx.font = font;\n    const t = ctx.measureText(\"Hg\");\n    asc = t.fontBoundingBoxAscent / FONT_PX;\n    desc = t.fontBoundingBoxDescent / FONT_PX;\n  }\n  return {\n    asc,\n    desc,\n    w: (s) => {\n      if (cache[s] === undefined) {\n        if (!ctx) return s.length * 0.5;\n        ctx.font = font;\n        cache[s] =\n          ctx.measureText(s).width / FONT_PX + TRACK * Array.from(s).length;\n      }\n      return cache[s];\n    },\n  };\n}\n\nfunction useMeasure() {\n  const [version, setVersion] = useState(0);\n  const [handle] = useState(() => delayRender(\"focus-front-swap: measuring\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    const bump = () => {\n      if (live) setVersion((v) => v + 1);\n    };\n    document.fonts.addEventListener(\"loadingdone\", bump);\n    Promise.all([\n      sans.waitUntilDone(),\n      manrope.waitUntilDone(),\n      document.fonts.load(`300 100px \"${sans.fontFamily}\"`),\n      document.fonts.load(`600 100px \"${manrope.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        bump();\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n      document.fonts.removeEventListener(\"loadingdone\", bump);\n    };\n  }, [handle]);\n  return useMemo(() => buildMeasure(version), [version]);\n}\n\ninterface Word {\n  text: string;\n  mask: boolean[];\n}\n\nfunction parse(text: string): Word[] {\n  const out: Word[] = [];\n  let on = false;\n  for (const raw of text.split(\" \").filter(Boolean)) {\n    let t = \"\";\n    const mask: boolean[] = [];\n    for (const ch of Array.from(raw)) {\n      if (ch === \"{\") on = true;\n      else if (ch === \"}\") on = false;\n      else {\n        t += ch;\n        mask.push(on);\n      }\n    }\n    if (t) out.push({ text: t, mask });\n  }\n  return out;\n}\n\nfunction breakRows(widths: number[], gap: number, k: number) {\n  const n = widths.length;\n  const span = (a: number, b: number) =>\n    widths.slice(a, b).reduce((s, w) => s + w, 0) + gap * (b - a - 1);\n  const best: number[][] = [];\n  const cut: number[][] = [];\n  for (let j = 0; j <= k; j++) {\n    best.push(new Array(n + 1).fill(Number.POSITIVE_INFINITY));\n    cut.push(new Array(n + 1).fill(0));\n  }\n  best[0][0] = 0;\n  for (let j = 1; j <= k; j++) {\n    for (let b = 1; b <= n; b++) {\n      for (let a = j - 1; a < b; a++) {\n        const v = Math.max(best[j - 1][a], span(a, b));\n        if (v < best[j][b]) {\n          best[j][b] = v;\n          cut[j][b] = a;\n        }\n      }\n    }\n  }\n  const rows: number[][] = [];\n  let end = n;\n  for (let j = k; j >= 1; j--) {\n    const a = cut[j][end];\n    rows.unshift(Array.from({ length: end - a }, (_, q) => a + q));\n    end = a;\n  }\n  return { rows, width: best[k][n] };\n}\n\ninterface Glyph {\n  ch: string;\n  x: number;\n  y: number;\n  accent: boolean;\n  u: number;\n}\n\nexport function FocusFrontSwap({\n  lines = [\n    \"Remocn knows the beat\",\n    \"You describe it once\",\n    \"Ship it {tonight}.\",\n  ],\n  accent,\n  ink,\n  logo = true,\n  theme = \"dark\",\n  ground,\n  groundParams,\n  swap = BEAT.swap,\n  blur = 18,\n}: FocusFrontSwapProps) {\n  const frame = useCurrentFrame();\n  const { width: W, height: H, fps, durationInFrames } = useVideoConfig();\n  const c = THEMES[theme];\n  const inkC = ink ?? c.ink;\n  const hi = accent ?? c.accent;\n  const preset = ground ?? (groundParams ? undefined : c.ground);\n  const gParams = groundParams ?? (ground ? undefined : c.groundParams);\n  const bg = gshParams(preset, gParams).background;\n  const m = useMeasure();\n  const portrait = H > W * 1.1;\n\n  const parsed = lines.map(parse);\n  const starts: number[] = [];\n  const lens: number[] = [];\n  let t = -9;\n  let len = swap;\n  parsed.forEach((ws, k) => {\n    if (k === 0) {\n      starts.push(t);\n      lens.push(BEAT.enter);\n      t += BEAT.enter;\n      return;\n    }\n    const prev = parsed[k - 1].length;\n    t += Math.max(24, Math.min(48, 8 + 4 * prev));\n    starts.push(t);\n    lens.push(len);\n    t += len;\n    len = Math.max(BEAT.minSwap, Math.round(len * BEAT.decay));\n    if (ws.length === 0) len = BEAT.minSwap;\n  });\n  const front = (k: number) => {\n    if (k < 0 || k >= starts.length) return -1;\n    const p = interpolate(frame, [starts[k], starts[k] + lens[k]], [0, 1], {\n      ...CLAMP,\n      easing: FRONT,\n    });\n    return -SOFT + (1 + 2 * SOFT) * p;\n  };\n  const lastEnd = starts[starts.length - 1] + lens[lens.length - 1];\n\n  const pan = parsed.reduce((acc, _, k) => {\n    if (k === 0) return acc;\n    return (\n      acc +\n      interpolate(frame, [starts[k], starts[k] + lens[k]], [0, 1], {\n        ...CLAMP,\n        easing: FRONT,\n      })\n    );\n  }, 0);\n\n  let content: React.ReactNode = <div />;\n  if (m) {\n    const gap = m.w(\" \");\n    const roomW = W * (portrait ? 0.84 : 0.8);\n    const maxT = portrait ? W * 0.19 : H * 0.22;\n    const minT = portrait ? W * 0.095 : H * 0.085;\n    const layouts = parsed.map((ws) => {\n      const widths = ws.map((w) => m.w(w.text));\n      let lay = breakRows(widths, gap, 1);\n      for (let k = 2; k <= Math.min(ws.length, 4); k++) {\n        if (roomW / Math.max(lay.width, ...widths) >= minT) break;\n        lay = breakRows(widths, gap, k);\n      }\n      return { widths, ...lay };\n    });\n    const cy = H * (logo ? (portrait ? 0.47 : 0.46) : 0.5);\n\n    const sizes = layouts.map((lay) =>\n      Math.min(maxT, roomW / Math.max(lay.width, 0.1)),\n    );\n    const glyphSets: Glyph[][] = layouts.map((lay, k) => {\n      const ws = parsed[k];\n      const T = sizes[k];\n      const rowH = T * 1.2;\n      const out: Glyph[] = [];\n      const rows = lay.rows;\n      const top = cy - (rows.length * rowH) / 2;\n      rows.forEach((row, ri) => {\n        const rw =\n          row.reduce((a, i) => a + lay.widths[i], 0) + gap * (row.length - 1);\n        let x = W / 2 - (rw * T) / 2;\n        const base = top + ri * rowH + rowH / 2 + (m.asc - m.desc) * T * 0.5;\n        row.forEach((i, j) => {\n          if (j > 0) x += gap * T;\n          const word = ws[i].text;\n          const chars = Array.from(word);\n          chars.forEach((ch, q) => {\n            const pre = q === 0 ? 0 : m.w(chars.slice(0, q).join(\"\"));\n            const cw = m.w(chars.slice(0, q + 1).join(\"\")) - pre;\n            const nudge = q > 0 && /[.,!?;:]/.test(ch) ? 0.035 : 0;\n            out.push({\n              ch,\n              x: x + (pre + nudge) * T,\n              y: base,\n              accent: ws[i].mask[q] ?? false,\n              u: x + (pre + cw / 2) * T,\n            });\n          });\n          x += lay.widths[i] * T;\n        });\n      });\n      return out;\n    });\n\n    let L = Number.POSITIVE_INFINITY;\n    let R = Number.NEGATIVE_INFINITY;\n    for (const g of glyphSets.flat()) {\n      L = Math.min(L, g.u);\n      R = Math.max(R, g.u);\n    }\n    const span = Math.max(1, R - L);\n    let Tp = Number.POSITIVE_INFINITY;\n    let Bt = Number.NEGATIVE_INFINITY;\n    for (const g of glyphSets.flat()) {\n      Tp = Math.min(Tp, g.y);\n      Bt = Math.max(Bt, g.y);\n    }\n    const multi = Bt - Tp > 1;\n    const keyOf = (g: Glyph) => {\n      const xk = (g.u - L) / span;\n      return multi ? xk * 0.8 + ((g.y - Tp) / (Bt - Tp)) * 0.2 : xk;\n    };\n\n    const nodes: React.ReactNode[] = [];\n    glyphSets.forEach((gs, k) => {\n      const T = sizes[k];\n      const asc = m.asc * T;\n      const desc = m.desc * T;\n      const fIn = front(k);\n      const fOut = front(k + 1);\n      if (fIn <= -SOFT && k > 0) return;\n      gs.forEach((g, gi) => {\n        const u = keyOf(g);\n        const inLocal = Math.min(1, Math.max(0, (fIn - u) / SOFT));\n        const outLocal =\n          k + 1 < glyphSets.length\n            ? Math.min(1, Math.max(0, (fOut - u) / SOFT))\n            : 0;\n        const inP = interpolate(inLocal, [0.45, 1], [0, 1], {\n          ...CLAMP,\n          easing: GLYPH_IN,\n        });\n        const outP = interpolate(outLocal, [0, 0.55], [0, 1], {\n          ...CLAMP,\n          easing: GLYPH_OUT,\n        });\n        const op = inP * (1 - outP);\n        if (op < 0.004) return;\n        const b = (1 - inP) * blur + outP * blur;\n        const sc = 1 + 0.07 * (1 - inP) + 0.09 * outP;\n        nodes.push(\n          <span\n            key={`g${k}-${gi}`}\n            style={{\n              position: \"absolute\",\n              left: g.x,\n              top: g.y - asc,\n              height: asc + desc,\n              lineHeight: `${(asc + desc).toFixed(2)}px`,\n              fontFamily: sans.fontFamily,\n              fontWeight: 300,\n              fontSize: T,\n              letterSpacing: `${TRACK}em`,\n              whiteSpace: \"pre\",\n              color: g.accent ? hi : inkC,\n              opacity: op,\n              transformOrigin: `50% ${(asc * 0.7).toFixed(2)}px`,\n              transform: `scale(${sc.toFixed(4)})`,\n              filter: b > 0.05 ? `blur(${b.toFixed(2)}px)` : undefined,\n            }}\n          >\n            {g.ch}\n          </span>,\n        );\n      });\n    });\n\n    const logoIn = spring({\n      frame: frame - (lastEnd + BEAT.logo),\n      fps,\n      config: CHIP,\n    });\n    const logoE = Math.min(1, Math.max(0, logoIn));\n    const lastRows = layouts[layouts.length - 1].rows.length;\n    const lastT = sizes[sizes.length - 1];\n    const logoSize = Math.max(lastT * 0.38, H * 0.045);\n    const logoTop = cy + (lastRows * lastT * 1.2) / 2 + lastT * 0.34;\n\n    const push = interpolate(frame, [0, durationInFrames], [1, 1.04], CLAMP);\n    content = (\n      <AbsoluteFill style={{ transform: `scale(${push.toFixed(4)})` }}>\n        {nodes}\n        {logo ? (\n          <div\n            style={{\n              position: \"absolute\",\n              left: 0,\n              width: W,\n              top: logoTop,\n              display: \"flex\",\n              justifyContent: \"center\",\n            }}\n          >\n            <div\n              style={{\n                transform: `translateY(${((1 - logoIn) * logoSize * 0.8).toFixed(2)}px) scale(${(0.88 + 0.12 * logoIn).toFixed(4)})`,\n                opacity: logoE,\n                filter:\n                  logoE < 1\n                    ? `blur(${((1 - logoE) * 6).toFixed(2)}px)`\n                    : undefined,\n              }}\n            >\n              <RemocnLogo fontSize={logoSize} color={c.logo} />\n            </div>\n          </div>\n        ) : null}\n      </AbsoluteFill>\n    );\n  }\n\n  return (\n    <AbsoluteFill style={{ background: bg, overflow: \"hidden\" }}>\n      <GshGround\n        preset={preset}\n        params={gParams}\n        timeOffset={c.groundOffset}\n        timeScale={0.55}\n        style={{\n          transform: `translateX(${(-pan * W * 0.025).toFixed(2)}px) scale(1.14)`,\n        }}\n      />\n      <AbsoluteFill\n        style={{\n          background: `radial-gradient(${portrait ? \"95% 50%\" : \"70% 60%\"} at 50% 47%, rgba(${c.veil},${c.veilIn}) 0%, rgba(${c.veil},${c.veilOut}) 100%)`,\n        }}\n      />\n      {content}\n    </AbsoluteFill>\n  );\n}\n\nexport default FocusFrontSwap;\n",
      "type": "registry:component",
      "target": "components/remocn/focus-front-swap.tsx"
    }
  ],
  "type": "registry:component"
}
