{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "name-gloss",
  "title": "Name Gloss",
  "description": "Two big words, \"Brand kit\", each get a small gloss line and a closing teal sentence.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/name-gloss/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Geologica\";\nimport type React from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst geologica = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface NameGlossPart {\n  word: string;\n  gloss: string;\n}\n\nexport interface NameGlossProps {\n  parts?: NameGlossPart[];\n  why?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: { bg: \"#0E3B43\", ink: \"#FFFFFF\", accent: \"#4FE3D0\" },\n  light: { bg: \"#E8F6F5\", ink: \"#0B3138\", accent: \"#0F8C7E\" },\n} as const;\n\nconst DEFAULT_PARTS: NameGlossPart[] = [\n  { word: \"Brand\", gloss: \"Your colours, your type, your logo.\" },\n  { word: \"kit\", gloss: \"Packed into every scene you install.\" },\n];\nconst DEFAULT_WHY = \"So the film looks like yours from the first render.\";\n\nconst FONT_PX = 200;\nconst NAME_W = 500;\nconst BODY_W = 400;\nconst NAME_TRACK = -0.04;\nconst BODY_TRACK = -0.015;\nconst BODY_LH = 1.2;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_MOVE = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nconst NAME_IN = 18;\nconst NAME_HOLD = 14;\nconst MOVE = 24;\nconst WORD_STAGGER = 2;\nconst WORD_IN = 13;\nconst WHY_AFTER = 28;\nconst HOLD = 42;\nconst DIM = 0.38;\n\ntype Metrics = Record<string, number>;\nconst key = (w: number, s: string) => `${w}\\u0002${s}`;\n\nfunction measureAll(texts: string[]): Metrics {\n  const canvas = document.createElement(\"canvas\");\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) throw new Error(\"name-gloss: no 2d context\");\n  const out: Metrics = {};\n  for (const weight of [NAME_W, BODY_W]) {\n    ctx.font = `${weight} ${FONT_PX}px \"${geologica.fontFamily}\"`;\n    for (const s of texts) {\n      out[key(weight, s)] = ctx.measureText(s).width / FONT_PX;\n    }\n  }\n  return out;\n}\n\nfunction useMetrics(texts: string[]) {\n  const id = texts.join(\"\\u0001\");\n  const [state, setState] = useState<{ id: string; m: Metrics } | null>(null);\n  const [handle] = useState(() => delayRender(\"name-gloss: measuring\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      geologica.waitUntilDone(),\n      document.fonts.load(`${NAME_W} 100px \"${geologica.fontFamily}\"`),\n      document.fonts.load(`${BODY_W} 100px \"${geologica.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setState({ id, m: measureAll(id.split(\"\\u0001\")) });\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [id, handle]);\n  return state && state.id === id ? state.m : null;\n}\n\nconst splitWords = (s: string) => s.split(/\\s+/).filter(Boolean);\n\nfunction balanced(\n  words: string[],\n  width: (w: string) => number,\n  space: number,\n  limit: number,\n) {\n  const greedy = (lim: number) => {\n    const lines: string[][] = [];\n    let cur: string[] = [];\n    let acc = 0;\n    for (const w of words) {\n      const ww = width(w);\n      if (cur.length > 0 && acc + space + ww > lim) {\n        lines.push(cur);\n        cur = [w];\n        acc = ww;\n      } else {\n        acc = cur.length > 0 ? acc + space + ww : ww;\n        cur.push(w);\n      }\n    }\n    if (cur.length > 0) lines.push(cur);\n    return lines;\n  };\n  const count = greedy(limit).length;\n  let lo = 0;\n  let hi = limit;\n  for (let i = 0; i < 18; i++) {\n    const mid = (lo + hi) / 2;\n    if (greedy(mid).length <= count) hi = mid;\n    else lo = mid;\n  }\n  return greedy(hi);\n}\n\ninterface Placed {\n  x0: number;\n  y0: number;\n  x1: number;\n  y1: number;\n  colW: number;\n  gloss: string[][];\n  glossTop: number;\n}\n\nexport function NameGloss({\n  parts = DEFAULT_PARTS,\n  why = DEFAULT_WHY,\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: NameGlossProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, durationInFrames } = useVideoConfig();\n  const c = THEMES[theme];\n  const hi = accent ?? c.accent;\n  const stage =\n    background === \"transparent\" ? \"transparent\" : (background ?? c.bg);\n\n  const list = parts.length > 0 ? parts : DEFAULT_PARTS;\n  const m = useMetrics([\n    \" \",\n    ...list.map((p) => p.word),\n    ...list.flatMap((p) => splitWords(p.gloss)),\n    ...splitWords(why),\n  ]);\n\n  if (!m) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const n = list.length;\n  const portrait = H > W * 1.1;\n  const margin = W * (portrait ? 0.08 : 0.07);\n  const avail = W - margin * 2;\n  const gutter = W * (portrait ? 0.045 : 0.07);\n\n  const nW = (s: string) =>\n    m[key(NAME_W, s)] + NAME_TRACK * Array.from(s).length;\n  const bW = (s: string) =>\n    m[key(BODY_W, s)] + BODY_TRACK * Array.from(s).length;\n  const wordEm = list.map((p) => nW(p.word));\n  const nSpace = nW(\" \");\n  const bSpace = bW(\" \");\n\n  const span = durationInFrames * speed;\n  const glossStart = NAME_IN + NAME_HOLD + MOVE - 6;\n  const fixed = glossStart + WHY_AFTER + 24 + HOLD;\n  const glossGap =\n    n > 1 ? Math.min(44, Math.max(18, (span - fixed) / (n - 1))) : 0;\n  const glossAt = (i: number) => glossStart + i * glossGap;\n  const whyAt = glossAt(n - 1) + WHY_AFTER;\n  const moveAt = NAME_IN + NAME_HOLD;\n\n  const ease = (from: number, dur: number, curve = EASE_OUT) =>\n    interpolate(t, [from, from + dur], [0, 1], { ...CLAMP, easing: curve });\n\n  let G = H * (portrait ? 0.05 : 0.085);\n  let F = 0;\n  let whyF = 0;\n  let whyLines: string[][] = [];\n  let placed: Placed[] = [];\n  let whyTop = 0;\n\n  for (let attempt = 0; attempt < 40; attempt++) {\n    const whyWords = splitWords(why);\n    const whyEm = whyWords.reduce(\n      (a, w, k) => a + bW(w) + (k > 0 ? bSpace : 0),\n      0,\n    );\n    whyF = G * 1.08;\n    if (whyEm * whyF > avail && avail / whyEm >= Math.max(G * 0.78, H * 0.05)) {\n      whyF = avail / whyEm;\n    }\n    whyLines = balanced(whyWords, bW, bSpace, avail / whyF);\n    if (portrait) {\n      F = Math.min(H * 0.13, avail / Math.max(...wordEm));\n      let y = H * 0.1;\n      const stackH = n * F * 0.98;\n      let y0 = H * 0.42 - stackH / 2;\n      placed = list.map((p) => {\n        const gloss = balanced(splitWords(p.gloss), bW, bSpace, avail / G);\n        const item: Placed = {\n          x0: margin,\n          y0,\n          x1: margin,\n          y1: y,\n          colW: avail,\n          gloss,\n          glossTop: y + F * 1.08,\n        };\n        y += F * 1.08 + gloss.length * BODY_LH * G + F * 0.42;\n        y0 += F * 0.98;\n        return item;\n      });\n      whyTop = Math.max(y + G, H * 0.86 - whyLines.length * BODY_LH * whyF);\n      const bottom = whyTop + whyLines.length * BODY_LH * whyF;\n      if (bottom <= H * 0.93 || attempt === 39) break;\n    } else {\n      const sumEm = wordEm.reduce((a, b) => a + b, 0);\n      const room = avail - gutter * (n - 1);\n      F = Math.min(H * 0.3, room / sumEm);\n      const need = wordEm.map((e) => e * F);\n      const cols = need.map(() => 0);\n      let free = room;\n      let open = need.map((_, i) => i);\n      for (let pass = 0; pass < n; pass++) {\n        const share = free / Math.max(1, open.length);\n        const fixedNow = open.filter((i) => need[i] > share);\n        if (fixedNow.length === 0) {\n          for (const i of open) cols[i] = share;\n          break;\n        }\n        for (const i of fixedNow) {\n          cols[i] = need[i];\n          free -= need[i];\n        }\n        open = open.filter((i) => !fixedNow.includes(i));\n      }\n      const top = H * 0.1;\n      const lineW = wordEm.reduce((a, e, i) => a + e + (i > 0 ? nSpace : 0), 0);\n      let xNat = (W - lineW * F) / 2;\n      let xCol = margin;\n      const glossTop = top + F * 1.02;\n      placed = list.map((p, i) => {\n        const gloss = balanced(\n          splitWords(p.gloss),\n          bW,\n          bSpace,\n          (cols[i] - gutter * 0.3) / G,\n        );\n        const item: Placed = {\n          x0: xNat,\n          y0: (H - F) / 2,\n          x1: xCol,\n          y1: top,\n          colW: cols[i],\n          gloss,\n          glossTop,\n        };\n        xNat += (wordEm[i] + nSpace) * F;\n        xCol += cols[i] + gutter;\n        return item;\n      });\n      const glossBottom = Math.max(\n        ...placed.map((pl) => pl.glossTop + pl.gloss.length * BODY_LH * G),\n      );\n      whyTop = H * 0.88 - whyLines.length * BODY_LH * whyF;\n      const tall = placed.some((pl) => pl.gloss.length > 4);\n      if ((!tall && glossBottom + G * 1.2 <= whyTop) || attempt === 39) break;\n    }\n    G *= 0.95;\n  }\n\n  const move = ease(moveAt, MOVE, EASE_MOVE);\n  const nodes: React.ReactNode[] = [];\n\n  placed.forEach((pl, i) => {\n    const p = list[i];\n    const inE = ease(i * 3, NAME_IN);\n    const x = pl.x0 + (pl.x1 - pl.x0) * move;\n    const y = pl.y0 + (pl.y1 - pl.y0) * move;\n    const active = ease(glossAt(i) - 4, 10);\n    const done = ease(whyAt - 6, 14);\n    const waiting = ease(moveAt + MOVE - 8, 10);\n    const lit = Math.max(active, done);\n    const opacity = 1 - (1 - DIM) * waiting * (1 - lit);\n    nodes.push(\n      <div\n        key={`word-${p.word}-${i}`}\n        style={{\n          position: \"absolute\",\n          left: x - F * 0.05,\n          top: y - F * 0.12,\n          height: F * 1.3,\n          paddingLeft: F * 0.05,\n          paddingRight: F * 0.12,\n          overflow: \"hidden\",\n          fontFamily: geologica.fontFamily,\n          fontWeight: NAME_W,\n          fontSize: F,\n          letterSpacing: `${NAME_TRACK}em`,\n          lineHeight: `${F * 1.3}px`,\n          color: c.ink,\n          opacity,\n          whiteSpace: \"pre\",\n        }}\n      >\n        <span\n          style={{\n            display: \"inline-block\",\n            transform: `translateY(${((1 - inE) * F * 1.2).toFixed(2)}px)`,\n          }}\n        >\n          {p.word}\n        </span>\n      </div>,\n    );\n\n    const at = glossAt(i);\n    if (t < at - 1) return;\n    let wi = 0;\n    nodes.push(\n      <div\n        key={`gloss-${i}`}\n        style={{\n          position: \"absolute\",\n          left: pl.x1 + F * 0.03,\n          top: pl.glossTop,\n          width: pl.colW + gutter * 0.5,\n          fontFamily: geologica.fontFamily,\n          fontWeight: BODY_W,\n          fontSize: G,\n          letterSpacing: `${BODY_TRACK}em`,\n          lineHeight: `${BODY_LH * G}px`,\n          color: c.ink,\n          whiteSpace: \"pre\",\n        }}\n      >\n        {pl.gloss.map((line) => (\n          <div key={line.join(\" \")}>\n            {line.map((w, k) => {\n              const e = ease(at + wi++ * WORD_STAGGER, WORD_IN);\n              return (\n                <span\n                  key={`${w}-${k}`}\n                  style={{\n                    display: \"inline-block\",\n                    opacity: e * 0.86,\n                    transform: `translateY(${((1 - e) * G * 0.7).toFixed(2)}px)`,\n                  }}\n                >\n                  {k > 0 ? ` ${w}` : w}\n                </span>\n              );\n            })}\n          </div>\n        ))}\n      </div>,\n    );\n  });\n\n  if (why && t >= whyAt - 1) {\n    let wi = 0;\n    nodes.push(\n      <div\n        key=\"why\"\n        style={{\n          position: \"absolute\",\n          left: margin + F * 0.03,\n          top: whyTop,\n          width: avail + whyF,\n          fontFamily: geologica.fontFamily,\n          fontWeight: NAME_W,\n          fontSize: whyF,\n          letterSpacing: `${BODY_TRACK}em`,\n          lineHeight: `${BODY_LH * whyF}px`,\n          color: hi,\n          whiteSpace: \"pre\",\n        }}\n      >\n        {whyLines.map((line) => (\n          <div key={line.join(\" \")}>\n            {line.map((w, k) => {\n              const e = ease(whyAt + wi++ * WORD_STAGGER, WORD_IN);\n              return (\n                <span\n                  key={`${w}-${k}`}\n                  style={{\n                    display: \"inline-block\",\n                    opacity: e,\n                    transform: `translateY(${((1 - e) * whyF * 0.7).toFixed(2)}px)`,\n                  }}\n                >\n                  {k > 0 ? ` ${w}` : w}\n                </span>\n              );\n            })}\n          </div>\n        ))}\n      </div>,\n    );\n  }\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      {nodes}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/name-gloss.tsx"
    }
  ],
  "type": "registry:component"
}
