{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tategaki-turn",
  "title": "Tategaki Turn",
  "description": "Vertical Japanese-style text turns upright into a horizontal line, 'Read it both ways.'.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/tategaki-turn/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont as loadZenKaku } from \"@remotion/google-fonts/ZenKakuGothicNew\";\nimport type React from \"react\";\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\";\n\nconst kaku = loadZenKaku(\"normal\", { weights: [\"500\"], subsets: [\"latin\"] });\n\nexport interface TategakiTurnProps {\n  text?: string;\n  highlight?: number;\n  hold?: number;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst VERMILION = \"#E03C31\";\n\nconst THEMES = {\n  dark: { bg: \"#111111\", ink: \"#F5F1EA\" },\n  light: { bg: \"#F5F1EA\", ink: \"#111111\" },\n} as const;\n\nconst TRACK = -0.02;\nconst PITCH = 1.2;\nconst STRETCH = 0.22;\nconst COL = 1.34;\n\nconst COL_STAGGER = 5;\nconst LETTER_STAGGER = 2;\nconst LETTER_LEN = 11;\nconst DROP = 0.42;\nconst SWING_LEN = 36;\nconst LINE_STAGGER = 12;\nconst TRAVEL_LEN = 22;\nconst LINE = 1.16;\nconst TIER_GAP = 0.9;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_TRAVEL = Easing.bezier(0.45, 0, 0.2, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\ninterface WordMetrics {\n  chars: string[];\n  lefts: number[];\n  advs: number[];\n  width: number;\n}\n\ninterface Metrics {\n  words: WordMetrics[];\n  space: number;\n  cap: number;\n  base: number;\n  colW: number;\n}\n\nfunction measure(words: string[]): Metrics {\n  const canvas = document.createElement(\"canvas\");\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) throw new Error(\"tategaki-turn: no 2d context\");\n  ctx.font = `500 100px \"${kaku.fontFamily}\"`;\n  const w = (s: string) => ctx.measureText(s).width / 100;\n  const h = ctx.measureText(\"H\");\n  const asc = h.fontBoundingBoxAscent / 100;\n  const desc = h.fontBoundingBoxDescent / 100;\n  let colW = 0;\n  const out = words.map((word) => {\n    const chars = Array.from(word);\n    const lefts = chars.map(\n      (_c, k) => w(chars.slice(0, k).join(\"\")) + TRACK * k,\n    );\n    const advs = chars.map((ch) => {\n      const a = w(ch);\n      colW = Math.max(colW, a);\n      return a;\n    });\n    return {\n      chars,\n      lefts,\n      advs,\n      width: w(word) + TRACK * Math.max(0, chars.length - 1),\n    };\n  });\n  return {\n    words: out,\n    space: w(\" \"),\n    cap: h.actualBoundingBoxAscent / 100,\n    base: (1 - (asc + desc)) / 2 + asc,\n    colW,\n  };\n}\n\nfunction useMetrics(words: string[]) {\n  const [metrics, setMetrics] = useState<Metrics | null>(null);\n  const [handle] = useState(() => delayRender(\"tategaki-turn: measuring\"));\n  const released = useRef(false);\n  const key = JSON.stringify(words);\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      kaku.waitUntilDone(),\n      document.fonts.load(`500 100px \"${kaku.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setMetrics(measure(JSON.parse(key) as string[]));\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [key, handle]);\n  return metrics;\n}\n\nexport function TategakiTurn({\n  text = \"Read it both ways.\",\n  highlight,\n  hold = 16,\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: TategakiTurnProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, fps } = useVideoConfig();\n  const c = THEMES[theme];\n  const bg = background ?? c.bg;\n  const red = accent ?? VERMILION;\n\n  const words = useMemo(() => {\n    const ws = text.split(/\\s+/).filter(Boolean);\n    return ws.length ? ws : [\" \"];\n  }, [text]);\n  const m = useMetrics(words);\n\n  if (!m) {\n    return <div style={{ position: \"absolute\", inset: 0, background: bg }} />;\n  }\n\n  const n = m.words.length;\n  const hiIdx = highlight ?? n - 1;\n  const pitch = m.cap * PITCH;\n  const colPitch = Math.max(m.colW, m.cap) * COL;\n  const portrait = H > W;\n  const fitW = W * (portrait ? 0.88 : 0.86);\n  const widths = m.words.map((wd) => wd.width);\n  const lineW = (ids: number[]) =>\n    ids.reduce((acc, j) => acc + widths[j], 0) +\n    m.space * Math.max(0, ids.length - 1);\n  const greedy = (maxW: number) => {\n    const out: number[][] = [];\n    let cur: number[] = [];\n    for (let j = 0; j < n; j++) {\n      if (cur.length && lineW([...cur, j]) > maxW) {\n        out.push(cur);\n        cur = [j];\n      } else cur.push(j);\n    }\n    if (cur.length) out.push(cur);\n    return out;\n  };\n  const breakInto = (L: number) => {\n    let lo = Math.max(...widths);\n    let hi = lineW(m.words.map((_wd, j) => j));\n    for (let i = 0; i < 30; i++) {\n      const mid = (lo + hi) / 2;\n      if (greedy(mid).length <= L) hi = mid;\n      else lo = mid;\n    }\n    return greedy(hi);\n  };\n  const finalSize = (ls: number[][]) =>\n    Math.min(\n      fitW / Math.max(...ls.map(lineW)),\n      H * 0.26,\n      (H * 0.72) / (m.cap + (ls.length - 1) * LINE),\n    );\n  const maxLines = Math.min(n, portrait ? 3 : 2);\n  let lines = breakInto(1);\n  let best = { ls: lines, f: finalSize(lines) };\n  for (let L = 1; L <= maxLines; L++) {\n    const ls = breakInto(L);\n    const f = finalSize(ls);\n    if (f > best.f) best = { ls, f };\n    if (f >= Math.min(W, H) * (portrait ? 0.17 : 0.12)) {\n      best = { ls, f };\n      break;\n    }\n  }\n  lines = best.ls;\n  const Fh = best.f;\n\n  const lineOf: number[] = [];\n  const wordX: number[] = [];\n  const wordY: number[] = [];\n  lines.forEach((ids, l) => {\n    let x = 0;\n    for (const j of ids) {\n      lineOf[j] = l;\n      wordX[j] = x;\n      wordY[j] = l * LINE;\n      x += widths[j] + m.space;\n    }\n  });\n  const hCenter = (j: number, k: number) =>\n    wordX[j] + m.words[j].lefts[k] + m.words[j].advs[k] / 2;\n\n  const pivotV: { x: number; y: number }[] = [];\n  let tierY = 0;\n  let maxCols = 1;\n  let maxLen = 1;\n  lines.forEach((ids, l) => {\n    const longest = Math.max(...ids.map((j) => m.words[j].chars.length));\n    maxLen = Math.max(maxLen, longest);\n    maxCols = Math.max(maxCols, ids.length);\n    ids.forEach((j, i) => {\n      pivotV[j] = { x: (i - (ids.length - 1) / 2) * colPitch, y: tierY };\n    });\n    if (l < lines.length - 1) tierY += (longest - 1) * pitch + TIER_GAP + m.cap;\n    else tierY += (longest - 1) * pitch;\n  });\n  const stageH = tierY + m.cap;\n  const Fv = Math.min(\n    (H * 0.84) / stageH,\n    fitW / (maxCols * colPitch),\n    H * (portrait ? 0.16 : 0.22),\n  );\n\n  const lastStart = (n - 1) * COL_STAGGER + (maxLen - 1) * LETTER_STAGGER;\n  const turnAt = lastStart + LETTER_LEN + hold;\n\n  const swing = m.words.map((_wd, j) =>\n    spring({\n      frame: t - turnAt - lineOf[j] * LINE_STAGGER,\n      fps,\n      durationInFrames: SWING_LEN,\n      config: { damping: 15, stiffness: 90, mass: 1 },\n    }),\n  );\n  const travel = m.words.map((_wd, j) => {\n    const s0 = turnAt + lineOf[j] * LINE_STAGGER;\n    return interpolate(t, [s0 - 2, s0 + TRAVEL_LEN], [0, 1], {\n      ...CLAMP,\n      easing: EASE_TRAVEL,\n    });\n  });\n  const zoomP =\n    travel.reduce((acc, v) => acc + v, 0) / Math.max(1, travel.length);\n\n  interface Glyph {\n    j: number;\n    k: number;\n    ch: string;\n    x: number;\n    y: number;\n    adv: number;\n  }\n  const glyphs: Glyph[] = [];\n  m.words.forEach((wd, j) => {\n    const ph = { x: hCenter(j, 0), y: wordY[j] };\n    const pv = pivotV[j];\n    const theta = (Math.PI / 2) * (1 - swing[j]);\n    const px = pv.x + (ph.x - pv.x) * travel[j];\n    const py = pv.y + (ph.y - pv.y) * travel[j];\n    const bend = Math.min(Math.PI / 2, Math.max(0, theta));\n    const g = 1 + STRETCH * Math.sin(2 * bend);\n    const cos = Math.cos(theta);\n    wd.chars.forEach((ch, k) => {\n      const rv = k * pitch;\n      const rh = hCenter(j, k) - ph.x;\n      let d = g * Math.hypot(rh * cos, rv * Math.sin(theta));\n      if (cos > 0.001) d = Math.min(d, rh / cos);\n      glyphs.push({\n        j,\n        k,\n        ch,\n        x: px + d * cos,\n        y: py + d * Math.sin(theta),\n        adv: wd.advs[k],\n      });\n    });\n  });\n\n  let minX = Number.POSITIVE_INFINITY;\n  let maxX = Number.NEGATIVE_INFINITY;\n  let minY = Number.POSITIVE_INFINITY;\n  let maxY = Number.NEGATIVE_INFINITY;\n  for (const g of glyphs) {\n    minX = Math.min(minX, g.x - g.adv / 2);\n    maxX = Math.max(maxX, g.x + g.adv / 2);\n    minY = Math.min(minY, g.y - m.cap);\n    maxY = Math.max(maxY, g.y);\n  }\n  const F = Math.min(\n    Fv + (Fh - Fv) * zoomP,\n    fitW / (maxX - minX),\n    (H * 0.84) / (maxY - minY),\n  );\n  const marginX = W * (portrait ? 0.08 : 0.07);\n  const marginY = H * (portrait ? 0.07 : 0.09);\n  const oxV = W - marginX - maxX * F;\n  const oyV = marginY - minY * F;\n  const oxH = marginX - minX * F;\n  const oyH = H - marginY - maxY * F;\n  const ox = oxV + (oxH - oxV) * zoomP;\n  const oy = oyV + (oyH - oyV) * zoomP;\n\n  const nodes: React.ReactNode[] = glyphs.map((g) => {\n    const startAt = g.j * COL_STAGGER + g.k * LETTER_STAGGER - 3;\n    const rise = interpolate(t, [startAt, startAt + LETTER_LEN], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n    const fill: React.CSSProperties = { color: g.j === hiIdx ? red : c.ink };\n    return (\n      <div\n        key={`${g.j}-${g.k}`}\n        style={{\n          position: \"absolute\",\n          left: ox + (g.x - g.adv / 2) * F,\n          top: oy + (g.y - m.base - (1 - rise) * DROP) * F,\n          width: g.adv * F,\n          height: F * 1.4,\n          opacity: rise,\n          fontFamily: kaku.fontFamily,\n          fontWeight: 500,\n          fontSize: F,\n          lineHeight: `${F}px`,\n          textAlign: \"center\",\n          whiteSpace: \"pre\",\n          ...fill,\n        }}\n      >\n        {g.ch}\n      </div>\n    );\n  });\n\n  return (\n    <AbsoluteFill style={{ background: bg, overflow: \"hidden\" }}>\n      {nodes}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/tategaki-turn.tsx"
    }
  ],
  "type": "registry:component"
}
