{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"text-rewrite","type":"registry:component","title":"Text Rewrite","description":"Text rewrite animation, a typewriter line that deletes and retypes its ending. A line that writes itself a word at a time, comes forward and gets selected — then its opening is swept away by a clip that cuts letters in half as it passes, and a new ending is written on while the line slides to its new centre.","dependencies":["remotion","@remotion/google-fonts"],"docs":"Needs a Remotion project. If a frame renders black, the component is drawing on a transparent layer: put a background behind it, e.g. <AbsoluteFill style={{ background: \"#faf9f6\" }}><Component /></AbsoluteFill>. Pass mode=\"dark\" for dark frames.","registryDependencies":["https://kuchi.kalarislabs.com/r/kuchi-ui.json"],"files":[{"path":"components/kuchi/text-rewrite.tsx","type":"registry:component","target":"components/kuchi/text-rewrite.tsx","content":"/*! Portions Copyright (c) 2026 Sri Nath (snap-cn). MIT License. See THIRD_PARTY_LICENSES. */\n\"use client\";\r\n\r\nimport { loadFont as loadSans } from \"@remotion/google-fonts/Inter\";\r\nimport { useEffect, useRef, useState } from \"react\";\r\nimport {\r\n  AbsoluteFill,\r\n  continueRender,\r\n  delayRender,\r\n  Easing,\r\n  getRemotionEnvironment,\r\n  interpolate,\r\n  useCurrentFrame,\r\n  useVideoConfig,\r\n} from \"remotion\";\r\nimport {\r\n  mixOklch,\r\n  resolveFont,\r\n  type KuchiTheme,\r\n  useKuchiTheme,\r\n  withAlpha,\r\n} from \"@/lib/kuchi-ui\";\r\n\r\n// Loaded through @remotion/google-fonts, never a CSS variable — a Remotion\r\n// bundle has none of the app's CSS, so a `var(--font-…)` gets you the right face\r\n// in the Player and a fallback in the mp4 (design-system rule 4).\r\nconst { fontFamily: SANS } = loadSans(\"normal\", {\r\n  weights: [\"400\", \"500\", \"600\"],\r\n  subsets: [\"latin\"],\r\n});\r\n\r\n// --- The stage -------------------------------------------------------------\r\n//\r\n// The reference recording is 810×458 and every number below was read off its\r\n// frames, so there is no second conversion to get wrong.\r\n\r\nconst REF_W = 810;\r\nconst REF_H = 458;\r\n\r\n// --- Measured proportions --------------------------------------------------\r\n\r\n/** The line is set tight — a hair of negative tracking, tighter word gaps. */\r\nconst TRACKING = -0.012;\r\nconst WORD_GAP = -0.05;\r\n\r\n/**\r\n * The selection box, in em of the type it sits behind: `0.954` tall, bottom\r\n * `0.083` below the baseline, reaching `0.11` past the type on an unclipped end.\r\n *\r\n * Measured at full size: the box is y 200…245 against a baseline at 241, and\r\n * runs x 174…577 against ink at 180…572.\r\n */\r\nconst SEL_HEIGHT = 0.954;\r\nconst SEL_BELOW = 0.083;\r\nconst SEL_PAD = 0.11;\r\n\r\n/**\r\n * The mark: `0.4em` clear of the last visible letter, its centre sitting `0.4em`\r\n * above the baseline — measured, 19.5px above a baseline at 241, at a type size\r\n * of 48.\r\n *\r\n * `markSize` is the *box*, not the ink. The reference's mark inks 36×40px of a\r\n * 0.83em square; ours fills about 70% of its own box, so 1.15em of box puts the\r\n * same amount of ink on the line.\r\n */\r\nconst MARK_GAP = 0.4;\r\nconst MARK_SIZE = 1.15;\r\nconst MARK_RISE = 0.4;\r\n\r\n/**\r\n * The sheen, as `[position, × the base alpha]`.\r\n *\r\n * Measured on eight frames spanning three different box widths and two phases —\r\n * 402px selected, 272px after the rewrite, 193px mid-erase. It lands in the same\r\n * place every time: **deepest at 0.36, palest at 0.575, a 3.2× swing**. That is\r\n * a gradient painted on the box, not on the page, which is why it appears to\r\n * travel as the box is dragged and then eaten.\r\n */\r\nconst SHEEN: readonly (readonly [number, number])[] = [\r\n  [0, 0.93],\r\n  [0.1, 1.19],\r\n  [0.19, 1.24],\r\n  [0.3, 1.49],\r\n  [0.37, 1.56],\r\n  [0.44, 1.03],\r\n  [0.51, 0.57],\r\n  [0.59, 0.49],\r\n  [0.69, 0.59],\r\n  [0.78, 0.75],\r\n  [0.89, 1.03],\r\n  [1, 1.16],\r\n];\r\n\r\n/**\r\n * The curve the erase and the rewrite both run on.\r\n *\r\n * Fitted on the recording's own timestamps — it is a 60fps capture with eight\r\n * dropped frames, so a frame index is not a clock. The erase's clip edge over\r\n * 29 frames gives **cubic-bezier(0.419, −0.050, 0.184, 1.044)** at rms 0.0086 of\r\n * the travel; the rewrite, fitted independently on the line's *centre* rather\r\n * than on any edge, gives **cubic-bezier(0.427, 0.021, 0.193, 0.999)** at rms\r\n * 0.0059. Two different signals, two different halves of the gesture, the same\r\n * curve — so it is one ease, and this is it.\r\n *\r\n * It is an ease-in-out, and that is the whole character of the move: the edge\r\n * creeps for four frames, crosses the sentence in twelve, and settles for\r\n * twelve more. An ease-out would have made it look like a deletion. This looks\r\n * like something being read and then taken away.\r\n */\r\nconst SWEEP_EASE = Easing.bezier(0.42, 0, 0.19, 1);\r\n\r\n/**\r\n * The selection drag.\r\n *\r\n * Measured without touching the gradient at all: take nineteen fixed columns\r\n * across the line and find the frame the warmth arrives in each. The edge is\r\n * 28% across by 17ms, 54% by 33ms, 71% by 50ms, 91% by 100ms and 97% by 150ms.\r\n * That is an `easeOut` of degree **3.5 over 200ms** — a `quint` is far too fast\r\n * off the mark and misses the first two points by twenty points of travel.\r\n */\r\nconst DRAG_EASE = Easing.out(Easing.poly(3.5));\r\n\r\n/** A word's fade, and the settle of the line's width around it. */\r\nconst WORD_EASE = Easing.out(Easing.cubic);\r\n\r\n/**\r\n * The kuchi mark, the same outline `public/logo.svg` draws, in a `0 0 100 100`\r\n * box. `markPath` replaces it with any path drawn in the same box; an empty\r\n * string leaves the line bare.\r\n */\r\nexport const MARK_PATH =\r\n  \"M18.92 0H67.08Q86 0 86 18.92V67.08Q86 86 67.08 86H18.92Q0 86 0 67.08V18.92Q0 0 18.92 0Z\";\r\n\r\n// --- The timeline, as pure functions ---------------------------------------\r\n\r\n/**\r\n * How many words have landed, and how far the newest one has opened the line.\r\n *\r\n * The reference does **not** reflow when a word lands. Measured across all four\r\n * arrivals, both ends of the line move outward by the same amount over the same\r\n * five frames — +18, +8, +4, +2, +1 on the right and the mirror of that on the\r\n * left — while the centre never leaves 404 of 810. So the line's *width* is\r\n * tweened around a fixed centre and the new word is revealed by it, rather than\r\n * being dropped into a flow that jumps.\r\n */\r\nexport function landed(\r\n  t: number,\r\n  n: number,\r\n  o: { at: number; stagger: number; dur: number },\r\n): { count: number; grow: number } {\r\n  if (t < o.at) return { count: 0, grow: 0 };\r\n  // +1e-6: a cue lands on an exact multiple of the stagger, and in doubles\r\n  // 0.217 + 3 × 0.189 divides back to 2.9999999, which drops a word.\r\n  const raw = Math.floor((t - o.at) / o.stagger + 1e-6) + 1;\r\n  const count = Math.min(n, raw);\r\n  const cue = o.at + (count - 1) * o.stagger;\r\n  const grow =\r\n    o.dur <= 0\r\n      ? 1\r\n      : interpolate(t, [cue, cue + o.dur], [0, 1], {\r\n          extrapolateLeft: \"clamp\",\r\n          extrapolateRight: \"clamp\",\r\n          easing: WORD_EASE,\r\n        });\r\n  return { count, grow };\r\n}\r\n\r\n/**\r\n * A word's ink: its opacity, and how far it has cooled out of the accent.\r\n *\r\n * Every word arrives in the accent, holds it for one beat — the same 189ms as\r\n * the stagger — and cools over the next, so exactly one word is ever coloured\r\n * and it walks the line ahead of the reader. The last word has no next word to\r\n * hand off to: on the reference it cools *while the selection is dragged over\r\n * it*, in half the time the drag takes, and is dark by the time the line is\r\n * selected.\r\n */\r\nexport function wordInk(\r\n  t: number,\r\n  i: number,\r\n  o: {\r\n    at: number;\r\n    stagger: number;\r\n    dur: number;\r\n    coolDelay: number;\r\n    coolDur: number;\r\n    dragAt?: number;\r\n    dragDur?: number;\r\n    last?: boolean;\r\n  },\r\n): { opacity: number; cool: number } {\r\n  const cue = o.at + i * o.stagger;\r\n  if (t < cue) return { opacity: 0, cool: 0 };\r\n  const opacity = interpolate(t, [cue, cue + o.dur], [0, 1], {\r\n    extrapolateLeft: \"clamp\",\r\n    extrapolateRight: \"clamp\",\r\n    easing: WORD_EASE,\r\n  });\r\n  const useDrag = o.last === true && o.dragAt !== undefined;\r\n  const from = useDrag ? (o.dragAt as number) : cue + o.coolDelay;\r\n  const span = useDrag ? (o.dragDur ?? o.coolDur) / 2 : o.coolDur;\r\n  const cool = interpolate(t, [from, from + Math.max(1e-4, span)], [0, 1], {\r\n    extrapolateLeft: \"clamp\",\r\n    extrapolateRight: \"clamp\",\r\n  });\r\n  return { opacity, cool };\r\n}\r\n\r\n/**\r\n * The selection being dragged across, as a fraction of the line.\r\n *\r\n * 28% across at 17ms, 54% at 33ms, 71% at 50ms — see `DRAG_EASE` for how that\r\n * was measured without the gradient getting in the way. It starts on the same\r\n * frame the line comes forward.\r\n */\r\nexport function dragIn(t: number, o: { at: number; dur: number }): number {\r\n  if (o.dur <= 0) return t >= o.at ? 1 : 0;\r\n  return interpolate(t, [o.at, o.at + o.dur], [0, 1], {\r\n    extrapolateLeft: \"clamp\",\r\n    extrapolateRight: \"clamp\",\r\n    easing: DRAG_EASE,\r\n  });\r\n}\r\n\r\n/** A sweep on the measured ease-in-out. Both halves of the rewrite use it. */\r\nexport function sweep(t: number, o: { at: number; dur: number }): number {\r\n  if (o.dur <= 0) return t >= o.at ? 1 : 0;\r\n  return interpolate(t, [o.at, o.at + o.dur], [0, 1], {\r\n    extrapolateLeft: \"clamp\",\r\n    extrapolateRight: \"clamp\",\r\n    easing: SWEEP_EASE,\r\n  });\r\n}\r\n\r\n/**\r\n * The deselect: the highlight's left edge running out to meet its right one.\r\n *\r\n * Nine frames, and **linear to within 6% of the travel** — 0.109, 0.233, 0.400,\r\n * 0.567, 0.691, 0.778, 0.851 against a straight line's 0.22, 0.33, 0.44, 0.55,\r\n * 0.67, 0.78, 0.89. No named curve does meaningfully better over nine frames,\r\n * and nothing about it reads as an ease. The selection is simply dropped.\r\n */\r\nexport function deselect(t: number, o: { at: number; dur: number }): number {\r\n  if (o.dur <= 0) return t >= o.at ? 1 : 0;\r\n  return interpolate(t, [o.at, o.at + o.dur], [0, 1], {\r\n    extrapolateLeft: \"clamp\",\r\n    extrapolateRight: \"clamp\",\r\n  });\r\n}\r\n\r\n/**\r\n * The forward move, as a scale.\r\n *\r\n * On the reference this is a **cut**: 188px of ink on one frame and 453px on the\r\n * next, with nothing in between anywhere in the recording. `zoomDuration={0}`\r\n * is that exactly.\r\n *\r\n * It is not the default. At 30fps a step in scale does not read as *coming\r\n * forward*, it reads as an edit — so the default is a short ramp on a moderate\r\n * decelerate, pivoted on the measured baseline because the rasteriser gives\r\n * glyph origins no vertical sub-pixel precision and any pivot that moves the\r\n * baseline makes the type climb the grid in whole-pixel jumps.\r\n */\r\nexport function zoomScale(\r\n  t: number,\r\n  o: { at: number; dur: number; to: number },\r\n): number {\r\n  if (o.dur <= 0) return t >= o.at ? o.to : 1;\r\n  return interpolate(t, [o.at, o.at + o.dur], [1, o.to], {\r\n    extrapolateLeft: \"clamp\",\r\n    extrapolateRight: \"clamp\",\r\n    easing: Easing.bezier(0.2, 0.6, 0.35, 1),\r\n  });\r\n}\r\n\r\n/** The sheen as a CSS gradient, in `color` at `alpha` times each stop. */\r\nexport function sheenGradient(\r\n  color: string,\r\n  alpha: number,\r\n  angle: number,\r\n  stops: readonly (readonly [number, number])[] = SHEEN,\r\n): string {\r\n  const list = stops\r\n    .map(\r\n      ([u, k]) =>\r\n        `${withAlpha(color, Math.min(1, alpha * k))} ${(u * 100).toFixed(1)}%`,\r\n    )\r\n    .join(\", \");\r\n  return `linear-gradient(${angle}deg, ${list})`;\r\n}\r\n\r\n// --- Props -----------------------------------------------------------------\r\n\r\nexport interface TextRewriteProps {\r\n  /** The line that writes itself, one word at a time. */\r\n  headline?: string;\r\n  /**\r\n   * How many words off the end of `headline` survive the erase. The rest are\r\n   * swept away from the left, and `append` is typed onto what is left.\r\n   */\r\n  keep?: number;\r\n  /** What gets written onto the surviving words. Empty leaves them alone. */\r\n  append?: string;\r\n\r\n  /** Seconds at which the mark appears, ahead of the first word. */\r\n  markAt?: number;\r\n  /** Seconds at which the first word lands. */\r\n  wordAt?: number;\r\n  /** Seconds between words — also how long a word holds the accent. */\r\n  wordStagger?: number;\r\n  /** How long a word takes to fade up and open the line. */\r\n  wordDuration?: number;\r\n  /** How long a word holds the accent before it starts cooling. */\r\n  coolDelay?: number;\r\n  /** How long the accent takes to become the foreground. */\r\n  coolDuration?: number;\r\n\r\n  /** Seconds at which the line comes forward. */\r\n  cutAt?: number;\r\n  /** How long the forward move takes. 0 is the reference's single-frame cut. */\r\n  zoomDuration?: number;\r\n  /** How far forward it goes. */\r\n  zoom?: number;\r\n\r\n  /** Seconds at which the selection starts being dragged across. */\r\n  dragAt?: number;\r\n  /** How long the drag takes. */\r\n  dragDuration?: number;\r\n  /** Seconds at which the erase starts eating the line from the left. */\r\n  eraseAt?: number;\r\n  /** How long the erase takes. */\r\n  eraseDuration?: number;\r\n  /** Seconds at which `append` starts being written on. */\r\n  rewriteAt?: number;\r\n  /** How long the rewrite takes. */\r\n  rewriteDuration?: number;\r\n  /** Seconds at which the selection is dropped. */\r\n  deselectAt?: number;\r\n  /** How long the deselect takes. */\r\n  deselectDuration?: number;\r\n\r\n  /** Type size before the forward move, in stage px. */\r\n  fontSize?: number;\r\n  /** Weight of the line. Inter ships 400/500/600 here. */\r\n  fontWeight?: number;\r\n  /** Where the line sits down the frame, as a fraction. */\r\n  centerY?: number;\r\n  /** Letter-spacing, in em. */\r\n  tracking?: number;\r\n  /** Word-spacing, in em. */\r\n  wordGap?: number;\r\n\r\n  /** Base strength of the selection, before the sheen multiplies it. */\r\n  selectionOpacity?: number;\r\n  /** Angle of the sheen. 90 is the reference's — straight across. */\r\n  selectionAngle?: number;\r\n  /** Override the sheen shape: `[position, × base alpha]` pairs. */\r\n  selectionSheen?: readonly (readonly [number, number])[];\r\n  /** How far the selection reaches past the type on an unclipped end, in em. */\r\n  selectionPad?: number;\r\n\r\n  /** The mark that trails the line, as a path in a `0 0 100 100` box. */\r\n  markPath?: string;\r\n  /** Its colour, if it should differ from the accent. */\r\n  markColor?: string;\r\n  /** Its height, in em. 0 leaves the line bare. */\r\n  markSize?: number;\r\n\r\n  /** The soft wash behind the line. */\r\n  glow?: boolean;\r\n  /** Colour of the wash, if it should differ from the accent. */\r\n  glowColor?: string;\r\n  /** Strength of the wash. */\r\n  glowOpacity?: number;\r\n\r\n  /** The colour a word arrives in, and the colour the sheen is painted from. */\r\n  accentColor?: string;\r\n  /** Token overrides. */\r\n  theme?: Partial<KuchiTheme>;\r\n  /** Which token set to resolve against. */\r\n  mode?: \"light\" | \"dark\";\r\n  /**\r\n   * The face this scene paints its words in — a label from `fonts.ts`\r\n   * (\"Inter\", \"Space Grotesk\", \"Instrument Serif\") or a CSS family you have\r\n   * loaded yourself. Unset, the scene keeps the face it was designed around.\r\n   *\r\n   * Overrides `theme.fontFamily`, which is how a brand kit re-skins a whole\r\n   * timeline from one value.\r\n   */\r\n  fontFamily?: string;\r\n  /** Multiplies the frame clock. */\r\n  speed?: number;\r\n}\r\n\r\n/**\r\n * A line that writes itself, is selected, has its opening swept away and a new\r\n * ending written on — the gesture of a claim being corrected in front of you.\r\n *\r\n * Everything here came off the recording frame by frame. The three things worth\r\n * knowing before you change anything:\r\n *\r\n * 1. **The erase is a clip, not a deletion.** Letters are cut in half by a\r\n *    vertical edge as it passes — \"Or\" becomes \")r\" becomes \"r\" — and the words\r\n *    that survive never move. A word-by-word delete would have re-laid the line\r\n *    out on every step, and the recording plainly does not.\r\n * 2. **The rewrite is the same edge run the other way**, over a line that is\r\n *    sliding to its new centre at the same time. Fitted separately, the slide\r\n *    and the reveal land on one shared progress to within 0.001.\r\n * 3. **The mark rides the edge**, not the end of the line — it sits `0.4em`\r\n *    clear of the last *visible* letter through all of it, which is what keeps\r\n *    the gesture reading as one object rather than a line and a decoration.\r\n */\r\nexport function TextRewrite({\r\n  headline = \"Or just add kuchi\",\r\n  keep = 1,\r\n  append = \"registry\",\r\n\r\n  markAt = 0.117,\r\n  wordAt = 0.217,\r\n  wordStagger = 0.189,\r\n  wordDuration = 0.085,\r\n  coolDelay = 0.189,\r\n  coolDuration = 0.189,\r\n\r\n  cutAt = 1.033,\r\n  zoomDuration = 0,\r\n  zoom = 2.41,\r\n\r\n  dragAt = 1.033,\r\n  dragDuration = 0.2,\r\n  eraseAt = 1.45,\r\n  eraseDuration = 0.517,\r\n  rewriteAt = 2.0,\r\n  rewriteDuration = 0.533,\r\n  deselectAt = 2.85,\r\n  deselectDuration = 0.15,\r\n\r\n  fontSize = 20,\r\n  fontWeight = 500,\r\n  centerY = 0.4902,\r\n  tracking = TRACKING,\r\n  wordGap = WORD_GAP,\r\n\r\n  selectionOpacity = 0.21,\r\n  selectionAngle = 90,\r\n  selectionSheen = SHEEN,\r\n  selectionPad = SEL_PAD,\r\n\r\n  markPath = MARK_PATH,\r\n  markColor,\r\n  markSize = MARK_SIZE,\r\n\r\n  glow = true,\r\n  glowColor,\r\n  glowOpacity = 0.075,\r\n\r\n  accentColor,\r\n  theme,\r\n  mode,\r\n  fontFamily,\r\n  speed = 1,\r\n}: TextRewriteProps) {\r\n  const frame = useCurrentFrame();\r\n  const { width, height, fps } = useVideoConfig();\r\n  const t = useKuchiTheme(theme, mode);\r\n  const face = resolveFont(fontFamily ?? t.fontFamily) ?? SANS;\r\n  const accent = accentColor ?? t.primary;\r\n  const wash = glowColor ?? accent;\r\n  const mark = markColor ?? accent;\r\n\r\n  const now = (frame * speed) / fps;\r\n  const stageScale = Math.min(width / REF_W, height / REF_H);\r\n  const words = headline.split(/\\s+/).filter(Boolean);\r\n  const kept = Math.max(0, Math.min(words.length, Math.round(keep)));\r\n  const cut = words.length - kept;\r\n  const lineHeight = Math.round(fontSize * 1.35);\r\n\r\n  // --- measurement -------------------------------------------------------\r\n  //\r\n  // Every position below is arithmetic on real widths, because the gesture\r\n  // needs geometry the flow cannot give it: where the surviving words sat in\r\n  // the *old* layout, how far a clip has to travel to reach them, and where the\r\n  // mark goes when the last visible letter is halfway through being eaten. A\r\n  // canvas ruler is no use — it knows nothing about CSS tracking or\r\n  // word-spacing — so the widths come from a hidden copy of the real line.\r\n  const probeRef = useRef<HTMLSpanElement>(null);\r\n  const baselineRef = useRef<HTMLSpanElement>(null);\r\n  const [handle] = useState(() => delayRender(\"text-rewrite: measure line\"));\r\n  const [m, setM] = useState<{\r\n    word: number[];\r\n    space: number;\r\n    append: number;\r\n    baseline: number;\r\n  } | null>(null);\r\n\r\n  useEffect(() => {\r\n    const probe = probeRef.current;\r\n    if (!probe) {\r\n      continueRender(handle);\r\n      return;\r\n    }\r\n    const parts = Array.from(probe.querySelectorAll<HTMLElement>(\"[data-w]\"));\r\n    const word = parts\r\n      .filter((p) => p.dataset.w === \"word\")\r\n      .map((p) => p.offsetWidth);\r\n    const space =\r\n      parts.find((p) => p.dataset.w === \"space\")?.offsetWidth ??\r\n      fontSize * 0.28;\r\n    const app = parts.find((p) => p.dataset.w === \"append\")?.offsetWidth ?? 0;\r\n    setM({\r\n      word,\r\n      space,\r\n      append: app,\r\n      baseline: baselineRef.current?.offsetTop ?? lineHeight * 0.8,\r\n    });\r\n  }, [handle, fontSize, lineHeight]);\r\n\r\n  useEffect(() => {\r\n    if (m) continueRender(handle);\r\n  }, [m, handle]);\r\n\r\n  const W = m?.word ?? words.map((w) => w.length * fontSize * 0.5);\r\n  const SP = m?.space ?? fontSize * 0.28;\r\n  const APP = m?.append ?? append.length * fontSize * 0.5;\r\n  const baseline = m?.baseline ?? lineHeight * 0.8;\r\n\r\n  /** Left edge of word `i` inside the line, and the line's width at `n` words. */\r\n  const offset = (i: number) => W.slice(0, i).reduce((a, b) => a + b + SP, 0);\r\n  const runWidth = (n: number) => (n <= 0 ? 0 : offset(n) - SP);\r\n\r\n  // --- timeline ----------------------------------------------------------\r\n  const pad = fontSize * selectionPad;\r\n  const gap = fontSize * MARK_GAP;\r\n  const markW = fontSize * markSize;\r\n  const tail = markPath ? gap + markW : 0;\r\n\r\n  const { count, grow } = landed(now, words.length, {\r\n    at: wordAt,\r\n    stagger: wordStagger,\r\n    dur: wordDuration,\r\n  });\r\n  const drag = dragIn(now, { at: dragAt, dur: dragDuration });\r\n  const erase = sweep(now, { at: eraseAt, dur: eraseDuration });\r\n  const write = sweep(now, { at: rewriteAt, dur: rewriteDuration });\r\n  const drop = deselect(now, { at: deselectAt, dur: deselectDuration });\r\n  const scale = zoomScale(now, { at: cutAt, dur: zoomDuration, to: zoom });\r\n\r\n  // Phase 1–2 layout. The line's *centring* width tweens around a fixed centre\r\n  // as each word opens it — measured, the ink's centre never leaves 404 of 810\r\n  // while its ends move ±34 over five frames. The word itself is drawn at full\r\n  // width from its first frame and fades: on the reference it lands whole and\r\n  // pale, overlapping the mark, and the mark slides clear over the next four\r\n  // frames. Nothing is ever clipped while the line is being written.\r\n  const opening = count > 0 ? (count > 1 ? SP : 0) + (W[count - 1] ?? 0) : 0;\r\n  const grown = runWidth(Math.max(0, count - 1)) + opening * grow;\r\n  const drawn = runWidth(count);\r\n  const full = runWidth(words.length);\r\n\r\n  // Layout A: the opening line, centred. Layout B: what it is rewritten into.\r\n  const aLeft = REF_W / 2 - (full + tail) / 2;\r\n  const keptLeft = aLeft + offset(cut);\r\n  const keptWidth = full - offset(cut);\r\n  const newWidth = keptWidth + (append ? SP + APP : 0);\r\n  const bLeft = REF_W / 2 - (newWidth + tail) / 2;\r\n\r\n  // Before the rewrite the line sits where it was written; after it, where its\r\n  // new content belongs. Fitted separately, the slide and the reveal land on\r\n  // one shared progress to within 0.001 of the travel.\r\n  const rewriting = write > 0;\r\n  const left = rewriting\r\n    ? keptLeft + (bLeft - keptLeft) * write\r\n    : REF_W / 2 - (grown + tail) / 2;\r\n\r\n  // The clip window. Its left edge is the erase, its right edge the rewrite —\r\n  // and nothing else touches it, which is why a letter gets cut in half by the\r\n  // edge instead of being deleted and re-laid-out.\r\n  const k0 = rewriting ? left : left + offset(cut) * erase;\r\n  const k1 = rewriting\r\n    ? left + keptWidth + (append ? SP + APP : 0) * write\r\n    : left + drawn;\r\n\r\n  // The selection is its own rectangle, not a lining inside the window: the\r\n  // drag sweeps it across a line that is entirely visible the whole time, and\r\n  // the deselect runs its left edge out to meet its right one over a line the\r\n  // clip has finished with.\r\n  const selFrom = erase > 0 || rewriting ? k0 : left - pad;\r\n  const selTo = rewriting ? k1 : left + drawn + pad;\r\n  const s1 = selFrom + (selTo - selFrom) * drag;\r\n  const s0 = selFrom + (s1 - selFrom) * drop;\r\n  const selShown = drag > 0 && drop < 1;\r\n\r\n  // The mark rides the end of what is *visible*, not the end of the line.\r\n  const markX = rewriting ? k1 : left + grown;\r\n\r\n  // A cut has no in-between frame to judder on, so it pivots on the line's\r\n  // optical centre — which is what the reference does: its small line and its\r\n  // full-size line share a centre at y 224 of 458, not a baseline. A *ramp*\r\n  // does have in-between frames, and there the pivot has to be the measured\r\n  // baseline: the rasteriser gives glyph origins no vertical sub-pixel\r\n  // precision, so any pivot that moves the baseline makes the type climb the\r\n  // pixel grid in whole-pixel jumps.\r\n  const pivot = zoomDuration > 0 ? `${baseline}px` : \"50%\";\r\n\r\n  const isRendering = getRemotionEnvironment().isRendering;\r\n  const moving = now > cutAt && now < cutAt + zoomDuration;\r\n  const willChange =\r\n    !isRendering && moving ? (\"transform\" as const) : undefined;\r\n\r\n  const boxTop = centerY * REF_H - lineHeight / 2;\r\n  const textStyle = {\r\n    fontFamily: face,\r\n    fontSize,\r\n    fontWeight,\r\n    lineHeight: `${lineHeight}px`,\r\n    letterSpacing: `${tracking}em`,\r\n    wordSpacing: `${wordGap}em`,\r\n    whiteSpace: \"pre\" as const,\r\n    // Hinting bends each glyph so its stems land on whole pixels, so as the size\r\n    // slides every stem re-snaps and the letterforms boil. This draws the\r\n    // outline as it actually is.\r\n    textRendering: \"geometricPrecision\" as const,\r\n  };\r\n\r\n  return (\r\n    <AbsoluteFill style={{ background: t.background }}>\r\n      <div\r\n        style={{\r\n          position: \"absolute\",\r\n          left: \"50%\",\r\n          top: \"50%\",\r\n          width: REF_W,\r\n          height: REF_H,\r\n          transform: `translate(-50%, -50%) scale(${stageScale})`,\r\n          overflow: \"hidden\",\r\n        }}\r\n      >\r\n        {glow ? (\r\n          <div\r\n            style={{\r\n              position: \"absolute\",\r\n              inset: 0,\r\n              background: `radial-gradient(46% 34% at 50% ${centerY * 100}%, ${withAlpha(wash, glowOpacity)} 0%, ${withAlpha(wash, 0)} 100%)`,\r\n            }}\r\n          />\r\n        ) : null}\r\n\r\n        {/* The ruler. Hidden, laid out once, read for the widths the geometry\r\n            above is built from — and for the baseline, which is never guessed\r\n            from a line-height ratio. */}\r\n        <span\r\n          ref={probeRef}\r\n          aria-hidden\r\n          style={{\r\n            ...textStyle,\r\n            position: \"absolute\",\r\n            left: 0,\r\n            top: 0,\r\n            visibility: \"hidden\",\r\n            pointerEvents: \"none\",\r\n          }}\r\n        >\r\n          {words.map((word, i) => (\r\n            <span data-w=\"word\" key={`${word}-${i}`}>\r\n              {word}\r\n            </span>\r\n          ))}\r\n          <span data-w=\"space\"> </span>\r\n          <span data-w=\"append\">{append}</span>\r\n          <span\r\n            ref={baselineRef}\r\n            style={{ display: \"inline-block\", width: 0, height: 0 }}\r\n          />\r\n        </span>\r\n\r\n        {/* The line, its selection and its mark in one group, so the forward\r\n            move carries all three. The scale is pivoted on the measured\r\n            baseline: the rasteriser gives glyph origins no vertical sub-pixel\r\n            precision, so any pivot that moves the baseline makes the type climb\r\n            the pixel grid in whole-pixel jumps. */}\r\n        <div\r\n          style={{\r\n            position: \"absolute\",\r\n            left: 0,\r\n            right: 0,\r\n            top: boxTop,\r\n            height: lineHeight,\r\n            transform: `scale(${scale})`,\r\n            transformOrigin: `50% ${pivot}`,\r\n            willChange,\r\n          }}\r\n        >\r\n          {/* The selection. Its own rectangle, outside the window, because it\r\n              is painted over a line the window is not yet touching. */}\r\n          {selShown ? (\r\n            <div\r\n              style={{\r\n                position: \"absolute\",\r\n                left: s0,\r\n                top: baseline + fontSize * SEL_BELOW - fontSize * SEL_HEIGHT,\r\n                width: Math.max(0, s1 - s0),\r\n                height: fontSize * SEL_HEIGHT,\r\n                background: sheenGradient(\r\n                  accent,\r\n                  selectionOpacity,\r\n                  selectionAngle,\r\n                  selectionSheen,\r\n                ),\r\n              }}\r\n            />\r\n          ) : null}\r\n\r\n          {/* The window. `overflow: hidden` is the erase and the rewrite both —\r\n              the text inside never moves relative to the line, so a letter is\r\n              cut in half by the edge rather than removed. */}\r\n          <div\r\n            style={{\r\n              position: \"absolute\",\r\n              left: k0,\r\n              top: 0,\r\n              width: Math.max(0, k1 - k0),\r\n              height: lineHeight,\r\n              overflow: \"hidden\",\r\n            }}\r\n          >\r\n            <div\r\n              style={{\r\n                ...textStyle,\r\n                position: \"absolute\",\r\n                left: left - k0,\r\n                top: 0,\r\n                width: \"max-content\",\r\n              }}\r\n            >\r\n              {(rewriting\r\n                ? [...words.slice(cut), ...(append ? [append] : [])]\r\n                : words.slice(0, count)\r\n              ).map((word, i) => {\r\n                const idx = rewriting ? cut + i : i;\r\n                const ink = wordInk(now, idx, {\r\n                  at: wordAt,\r\n                  stagger: wordStagger,\r\n                  dur: wordDuration,\r\n                  coolDelay,\r\n                  coolDur: coolDuration,\r\n                  dragAt,\r\n                  dragDur: dragDuration,\r\n                  last: idx === words.length - 1,\r\n                });\r\n                return (\r\n                  <span\r\n                    // biome-ignore lint/suspicious/noArrayIndexKey: words repeat, so the index is what makes each key unique\r\n                    key={`${word}-${i}`}\r\n                    style={{\r\n                      color: rewriting\r\n                        ? t.foreground\r\n                        : mixOklch(accent, t.foreground, ink.cool),\r\n                      opacity: rewriting ? 1 : ink.opacity,\r\n                    }}\r\n                  >\r\n                    {i > 0 ? \" \" : null}\r\n                    {word}\r\n                  </span>\r\n                );\r\n              })}\r\n            </div>\r\n          </div>\r\n\r\n          {/* The mark rides the window's right edge, not the end of the line. */}\r\n          {markPath && now >= markAt ? (\r\n            <svg\r\n              viewBox=\"0 0 100 100\"\r\n              width={markW}\r\n              height={markW}\r\n              aria-hidden\r\n              style={{\r\n                position: \"absolute\",\r\n                left: markX + gap,\r\n                top: baseline - fontSize * MARK_RISE - markW / 2,\r\n                overflow: \"visible\",\r\n              }}\r\n            >\r\n              <path d={markPath} fill={mark} />\r\n            </svg>\r\n          ) : null}\r\n        </div>\r\n      </div>\r\n    </AbsoluteFill>\r\n  );\r\n}\r\n\r\nexport default TextRewrite;\r\n"}]}