{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "serif-rise",
  "title": "Serif Rise",
  "description": "A large serif line rises into place, 'From rough cut to final cut.', end phrase in red.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/serif-rise/index.tsx",
      "content": "\"use client\";\n\nimport { loadVariableFont } from \"@remotion/google-fonts/BricolageGrotesque\";\nimport type React from \"react\";\nimport { useEffect, useId, useMemo, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst gothic = loadVariableFont(\"normal\", { subsets: [\"latin\"] });\n\nexport interface SerifRiseProps {\n  lines?: string[];\n  highlight?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: {\n    bg: \"#16130F\",\n    ink: \"#F2EFE8\",\n    accent: \"#E5342A\",\n    rule: \"rgba(242,239,232,0.2)\",\n    grain: 0.05,\n  },\n  light: {\n    bg: \"#F2EFE8\",\n    ink: \"#111111\",\n    accent: \"#E5342A\",\n    rule: \"rgba(17,17,17,0.18)\",\n    grain: 0.035,\n  },\n} as const;\n\nconst W_START = 500;\nconst W_END = 380;\nconst WGHT = W_END;\nconst CTRS_MAX = 1;\nconst TRACK = -0.022;\nconst LINE = 1.02;\nconst HAIR = 0.03;\nconst REACH = 0.62;\n\nconst RISE_LEN = 18;\nconst RISE_GAP = 1.5;\nconst SWEEP_AT = 32;\nconst SWEEP_GAP = 2;\nconst SWEEP_SPAN = 44;\nconst SWEEP_LEN = 16;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_CTRS = Easing.bezier(0.33, 0, 0.2, 1);\nconst EASE_SERIF = Easing.bezier(0.3, 1.45, 0.5, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nconst fvs = (k: number) =>\n  `\"wght\" ${(W_START + (W_END - W_START) * k).toFixed(1)}, \"opsz\" 96, \"wdth\" 100`;\n\ntype Side = \"none\" | \"both\" | \"left\" | \"right\";\ninterface Stem {\n  cx: number;\n  w: number;\n  foot: Side;\n  head: Side;\n  top: number;\n}\ninterface Metrics {\n  adv: Record<string, [number, number]>;\n  kern: Record<string, [number, number]>;\n  stems: Record<string, Stem[]>;\n  cap: number;\n  baseline: number;\n}\n\nconst RASTER = 300;\n\nfunction runsAt(\n  data: Uint8ClampedArray,\n  W: number,\n  y: number,\n): [number, number][] {\n  const out: [number, number][] = [];\n  let start = -1;\n  for (let x = 0; x < W; x++) {\n    const on = data[(y * W + x) * 4 + 3] > 110;\n    if (on && start < 0) start = x;\n    if (!on && start >= 0) {\n      out.push([start, x]);\n      start = -1;\n    }\n  }\n  if (start >= 0) out.push([start, W]);\n  return out;\n}\n\nfunction findStems(\n  ctx: CanvasRenderingContext2D,\n  ch: string,\n  xh: number,\n  cap: number,\n  asc: number,\n): Stem[] {\n  const S = RASTER;\n  const pad = Math.round(S * 0.3);\n  const advPx = ctx.measureText(ch).width;\n  const W = Math.ceil(advPx + pad * 2);\n  const H = Math.ceil(S * 1.6);\n  const base = Math.round(S * 1.15);\n  ctx.canvas.width = W;\n  ctx.canvas.height = H;\n  ctx.font = `${WGHT} ${S}px \"${gothic.fontFamily}\"`;\n  ctx.textBaseline = \"alphabetic\";\n  ctx.fillStyle = \"#000\";\n  ctx.fillText(ch, pad, base);\n  const data = ctx.getImageData(0, 0, W, H).data;\n  const alpha = (x: number, y: number) =>\n    x < 0 || x >= W || y < 0 || y >= H ? 0 : data[(y * W + x) * 4 + 3];\n  const upper = ch !== ch.toLowerCase() || /[0-9]/.test(ch);\n  const near = (a: number, b: number, tol: number) => Math.abs(a - b) <= tol;\n  const y1 = base - 3;\n  const y2 = base - Math.round(xh * S * 0.22);\n  const y3 = base - Math.round(xh * S * 0.48);\n  const r1s = runsAt(data, W, y1);\n  const r3s = runsAt(data, W, y3);\n  const stems: Stem[] = [];\n  for (const [a, b] of runsAt(data, W, y2)) {\n    const w = b - a;\n    const cx = (a + b) / 2;\n    if (w > S * 0.2 || w < S * 0.02) continue;\n    const r3 = r3s.find(([p, q]) => near((p + q) / 2, cx, w * 0.2));\n    if (!r3 || !near(r3[1] - r3[0], w, w * 0.3)) continue;\n    let foot: Side = \"none\";\n    const r1 = r1s.find(([p, q]) => p <= cx && q >= cx);\n    if (r1 && alpha(Math.round(cx), base + 5) < 40) {\n      const w1 = r1[1] - r1[0];\n      if (near(w1, w, w * 0.3)) foot = \"both\";\n      else if (near(r1[0], a, w * 0.25)) foot = \"left\";\n      else if (near(r1[1], b, w * 0.25)) foot = \"right\";\n    }\n    let y = y2;\n    while (y > 1 && alpha(Math.round(cx), y - 1) > 110) y--;\n    const top = (base - y) / S;\n    let head: Side = \"none\";\n    const atHeight =\n      near(top, xh, 0.05) || near(top, cap, 0.05) || near(top, asc, 0.06);\n    if (atHeight && !\"tf\".includes(ch)) {\n      const rt = runsAt(data, W, y + 3).find(([p, q]) => p <= cx && q >= cx);\n      if (rt) {\n        const wt = rt[1] - rt[0];\n        if (near(wt, w, w * 0.35)) head = upper ? \"both\" : \"left\";\n        else if (near(rt[0], a, w * 0.25)) head = \"left\";\n        else if (near(rt[1], b, w * 0.25) && upper) head = \"right\";\n      }\n    }\n    if (foot === \"none\" && head === \"none\") continue;\n    stems.push({ cx: (cx - pad) / S, w: w / S, foot, head, top });\n  }\n  return stems;\n}\n\nfunction measure(lines: string[]): Metrics {\n  const probe = document.createElement(\"span\");\n  Object.assign(probe.style, {\n    position: \"absolute\",\n    left: \"-99999px\",\n    top: \"0\",\n    visibility: \"hidden\",\n    whiteSpace: \"pre\",\n    fontFamily: `\"${gothic.fontFamily}\"`,\n    fontStyle: \"normal\",\n    fontSize: \"100px\",\n    lineHeight: \"1\",\n  });\n  document.body.appendChild(probe);\n  const widthOf = (s: string, ctrs: number) => {\n    probe.style.fontVariationSettings = fvs(ctrs);\n    probe.textContent = s;\n    return probe.getBoundingClientRect().width / 100;\n  };\n  const adv: Metrics[\"adv\"] = {};\n  const kern: Metrics[\"kern\"] = {};\n  for (const line of lines) {\n    const chars = Array.from(line);\n    chars.forEach((ch, i) => {\n      if (!adv[ch]) adv[ch] = [widthOf(ch, 0), widthOf(ch, CTRS_MAX)];\n      const next = chars[i + 1];\n      if (next) {\n        const pair = ch + next;\n        if (!kern[pair]) {\n          kern[pair] = [0, CTRS_MAX].map((c) => {\n            const k = widthOf(pair, c) - widthOf(ch, c) - widthOf(next, c);\n            return Math.abs(k) < 0.002 ? 0 : k;\n          }) as [number, number];\n        }\n      }\n    });\n  }\n  probe.remove();\n\n  const ctx = document\n    .createElement(\"canvas\")\n    .getContext(\"2d\", { willReadFrequently: true });\n  const stems: Metrics[\"stems\"] = {};\n  let cap = 0.7;\n  let baseline = 0.8;\n  if (ctx) {\n    ctx.font = `${WGHT} 100px \"${gothic.fontFamily}\"`;\n    const fH = ctx.measureText(\"H\");\n    cap = fH.actualBoundingBoxAscent / 100;\n    const xh = ctx.measureText(\"x\").actualBoundingBoxAscent / 100;\n    const asc = ctx.measureText(\"l\").actualBoundingBoxAscent / 100;\n    const fa = fH.fontBoundingBoxAscent / 100;\n    const fd = fH.fontBoundingBoxDescent / 100;\n    baseline = (1 - (fa + fd)) / 2 + fa;\n    for (const ch of Object.keys(adv)) {\n      stems[ch] = ch.trim() ? findStems(ctx, ch, xh, cap, asc) : [];\n    }\n  }\n  return { adv, kern, stems, cap, baseline };\n}\n\nfunction useMetrics(lines: string[]) {\n  const key = lines.join(\"\\u0001\");\n  const [state, setState] = useState<{ key: string; m: Metrics } | null>(null);\n  const [handle] = useState(() => delayRender(\"serif-rise: measuring\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      gothic.waitUntilDone(),\n      document.fonts.load(`${WGHT} 100px \"${gothic.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setState({ key, m: measure(key.split(\"\\u0001\")) });\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [key, handle]);\n  return state && state.key === key ? state.m : null;\n}\n\nfunction Grain({ opacity }: { opacity: number }) {\n  const id = `sr${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  return (\n    <svg\n      width=\"100%\"\n      height=\"100%\"\n      style={{ position: \"absolute\", inset: 0, opacity, pointerEvents: \"none\" }}\n      aria-hidden\n    >\n      <filter id={id}>\n        <feTurbulence\n          type=\"fractalNoise\"\n          baseFrequency=\"0.9\"\n          numOctaves={2}\n          seed={7}\n          stitchTiles=\"stitch\"\n        />\n        <feColorMatrix values=\"0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0.9 0\" />\n      </filter>\n      <rect width=\"100%\" height=\"100%\" filter={`url(#${id})`} />\n    </svg>\n  );\n}\n\nexport function SerifRise({\n  lines = [\"From rough cut\", \"to final cut.\"],\n  highlight = \"final cut.\",\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: SerifRiseProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H } = useVideoConfig();\n  const c = THEMES[theme];\n  const spot = accent ?? c.accent;\n  const rows = useMemo(\n    () => (lines.length > 0 ? lines : [\"final cut.\"]).map((l) => l || \" \"),\n    [lines],\n  );\n  const m = useMetrics(rows);\n\n  const transparent = background === \"transparent\";\n  const stage = transparent ? \"transparent\" : (background ?? c.bg);\n\n  if (!m) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const lerp = (a: number, b: number, k: number) => a + (b - a) * k;\n  const lineWidth = (s: string, k: number) => {\n    const chars = Array.from(s);\n    return chars.reduce((acc, ch, i) => {\n      const a = m.adv[ch];\n      const kn = i < chars.length - 1 ? m.kern[ch + chars[i + 1]] : null;\n      return (\n        acc +\n        lerp(a[0], a[1], k) +\n        (kn ? lerp(kn[0], kn[1], k) : 0) +\n        (i < chars.length - 1 ? TRACK : 0)\n      );\n    }, 0);\n  };\n  const widest = Math.max(\n    ...rows.map((r) => Math.max(lineWidth(r, 0), lineWidth(r, 1))),\n  );\n\n  const portrait = H > W * 1.1;\n  const edge = W * (portrait ? 0.05 : 0.035);\n  const F = Math.min(\n    (W - edge * 2) / widest,\n    (H * (portrait ? 0.5 : 0.78)) / (rows.length * LINE),\n  );\n  const lineH = F * LINE;\n  const capPx = m.cap * F;\n  const blockH = (rows.length - 1) * lineH + capPx;\n  const firstBase = (H - blockH) / 2 + capPx;\n  const ruleH = Math.max(1.5, F * 0.008);\n\n  const hlRanges = rows.map((r) => {\n    if (!highlight) return null;\n    const at = r.indexOf(highlight);\n    return at >= 0 ? [at, at + Array.from(highlight).length] : null;\n  });\n\n  const ruleNodes: React.ReactNode[] = [];\n  const glyphNodes: React.ReactNode[] = [];\n  const serifNodes: React.ReactNode[] = [];\n  const total = rows.reduce((a, r) => a + Array.from(r).length, 0);\n  const gap = Math.min(SWEEP_GAP, SWEEP_SPAN / Math.max(1, total - 1));\n  let gi = 0;\n  rows.forEach((row, r) => {\n    const chars = Array.from(row);\n    const baseY = firstBase + r * lineH;\n    const ruleP = interpolate(t, [r * 4, r * 4 + 22], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n    ruleNodes.push(\n      <div\n        key={`rule${r}`}\n        style={{\n          position: \"absolute\",\n          left: 0,\n          top: baseY + ruleH * 0.5,\n          width: W,\n          height: ruleH,\n          background: c.rule,\n          transformOrigin: \"0% 50%\",\n          transform: `scaleX(${ruleP.toFixed(4)})`,\n        }}\n      />,\n    );\n    const progress = chars.map((ch, i) => {\n      const idx = gi + i;\n      const p = interpolate(\n        t,\n        [SWEEP_AT + idx * gap, SWEEP_AT + idx * gap + SWEEP_LEN],\n        [0, 1],\n        CLAMP,\n      );\n      return {\n        ctrs: EASE_CTRS(p),\n        serif: p <= 0.3 ? 0 : EASE_SERIF(Math.min(1, (p - 0.3) / 0.7)),\n        ch,\n      };\n    });\n    let x = 0;\n    const xs = progress.map(({ ch, ctrs }, i) => {\n      const gx = x;\n      const a = m.adv[ch];\n      const kn = i < chars.length - 1 ? m.kern[ch + chars[i + 1]] : null;\n      x += lerp(a[0], a[1], ctrs) + (kn ? lerp(kn[0], kn[1], ctrs) : 0) + TRACK;\n      return gx;\n    });\n    const left = edge;\n    const hl = hlRanges[r];\n    const hlFill = (i: number) => hl !== null && i >= hl[0] && i < hl[1];\n    const colorFor = (i: number) => (hlFill(i) ? spot : c.ink);\n\n    progress.forEach(({ ch, ctrs, serif }, i) => {\n      const idx = gi + i;\n      const gxPx = left + xs[i] * F;\n      const rise = interpolate(\n        t,\n        [idx * RISE_GAP, idx * RISE_GAP + RISE_LEN],\n        [0, 1],\n        { ...CLAMP, easing: EASE_OUT },\n      );\n      const dy = (1 - rise) * F * 0.9;\n      glyphNodes.push(\n        <div\n          key={`m${r}-${i}`}\n          style={{\n            position: \"absolute\",\n            left: gxPx - F * 0.2,\n            top: baseY - capPx * 1.5,\n            width: m.adv[ch][0] * F + F * 0.6,\n            height: capPx * 1.5 + F * 0.3,\n            overflow: \"hidden\",\n            clipPath: `inset(0 0 ${rise < 1 ? (F * 0.3 - F * 0.03).toFixed(1) : 0}px 0)`,\n          }}\n        >\n          <span\n            style={{\n              position: \"absolute\",\n              left: F * 0.2,\n              top: capPx * 1.5 - m.baseline * F,\n              height: F,\n              lineHeight: `${F}px`,\n              whiteSpace: \"pre\",\n              fontFamily: gothic.fontFamily,\n              fontStyle: \"normal\",\n              fontSize: F,\n              fontVariationSettings: fvs(CTRS_MAX * ctrs),\n              transform: `translateY(${dy.toFixed(2)}px)`,\n              color: colorFor(i),\n            }}\n          >\n            {ch}\n          </span>\n        </div>,\n      );\n      if (serif <= 0.001) return;\n      const a = m.adv[ch];\n      const scale = lerp(a[0], a[1], ctrs) / Math.max(1e-6, a[1]);\n      const th = Math.max(1.5, HAIR * F);\n      const boxL = gxPx + F * 0.006;\n      const boxR = gxPx + lerp(a[0], a[1], ctrs) * F - F * 0.006;\n      m.stems[ch]?.forEach((s, k) => {\n        const cx = gxPx + s.cx * scale * F;\n        const w = s.w * F;\n        const reach = w * REACH * serif;\n        const bar = (side: Side, y: number, key: string) => {\n          if (side === \"none\") return;\n          const x0 = Math.max(\n            Math.min(boxL, cx - w / 2),\n            side === \"right\" ? cx : cx - w / 2 - reach,\n          );\n          const x1 = Math.min(\n            Math.max(boxR, cx + w / 2),\n            side === \"left\" ? cx : cx + w / 2 + reach,\n          );\n          serifNodes.push(\n            <div\n              key={key}\n              style={{\n                position: \"absolute\",\n                left: x0,\n                top: y,\n                width: x1 - x0,\n                height: th,\n                background: colorFor(i),\n              }}\n            />,\n          );\n        };\n        bar(s.foot, baseY - th, `f${r}-${i}-${k}`);\n        bar(s.head, baseY - s.top * F, `h${r}-${i}-${k}`);\n      });\n    });\n    gi += chars.length;\n  });\n\n  return (\n    <AbsoluteFill style={{ background: stage, overflow: \"hidden\" }}>\n      {ruleNodes}\n      {glyphNodes}\n      {serifNodes}\n      {transparent ? null : <Grain opacity={c.grain} />}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/serif-rise.tsx"
    }
  ],
  "type": "registry:component"
}
