{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-signature",
  "title": "Text Signature",
  "description": "Writes any text in any font as a signature: outlines drawn by the pen, then inked.",
  "dependencies": [
    "opentype.js@^2.0.0"
  ],
  "devDependencies": [
    "@types/opentype.js@^1.3.10"
  ],
  "registryDependencies": [
    "@corsair-ui/signature"
  ],
  "files": [
    {
      "path": "registry/default/ui/text-signature.tsx",
      "content": "\"use client\";\n\nimport type { Font, Path } from \"opentype.js\";\nimport { useEffect, useState } from \"react\";\n\nimport { Signature, type SignatureProps } from \"@/registry/default/ui/signature\";\n\n// One download and parse per font, however many signatures use it.\nconst fonts = new Map<string, Promise<Font>>();\n\nfunction loadFont(url: string) {\n  let font = fonts.get(url);\n  if (!font) {\n    font = (async () => {\n      const [{ parse }, response] = await Promise.all([import(\"opentype.js\"), fetch(url)]);\n      if (!response.ok) throw new Error(`Could not load the font at ${url} (${response.status}).`);\n      return parse(await response.arrayBuffer());\n    })();\n    // A failed download can be retried by the next signature that asks.\n    font.catch(() => fonts.delete(url));\n    fonts.set(url, font);\n  }\n  return font;\n}\n\n// Hundredths of a unit, and never in exponent notation at these sizes.\nconst round = (value: number) => String(Math.round(value * 100) / 100);\nconst point = (x: number, y: number) => `${round(x)} ${round(y)}`;\n\n/**\n * SVG path data for one glyph. Path#toPathData would do, except that in\n * opentype.js 2.0 it writes NaN for coordinates a hair above a whole number,\n * and browsers stop drawing a path at its first NaN.\n */\nfunction pathData(glyph: Path) {\n  return glyph.commands\n    .map((command) => {\n      switch (command.type) {\n        case \"M\":\n        case \"L\":\n          return command.type + point(command.x, command.y);\n        case \"Q\":\n          return `Q${point(command.x1, command.y1)} ${point(command.x, command.y)}`;\n        case \"C\":\n          return `C${point(command.x1, command.y1)} ${point(command.x2, command.y2)} ${point(command.x, command.y)}`;\n        case \"Z\":\n          return \"Z\";\n      }\n    })\n    .join(\"\");\n}\n\n/** One outline per glyph, in writing order, and a viewBox that fits them all. */\nfunction lettering(font: Font, text: string, fontSize: number) {\n  const baseline = font.ascender * (fontSize / font.unitsPerEm);\n  const glyphs = font.getPaths(text, 0, baseline, fontSize, { kerning: true });\n  let minX = Infinity;\n  let minY = Infinity;\n  let maxX = -Infinity;\n  let maxY = -Infinity;\n  const paths: string[] = [];\n  for (const glyph of glyphs) {\n    const d = pathData(glyph);\n    if (!d) continue;\n    const box = glyph.getBoundingBox();\n    minX = Math.min(minX, box.x1);\n    minY = Math.min(minY, box.y1);\n    maxX = Math.max(maxX, box.x2);\n    maxY = Math.max(maxY, box.y2);\n    paths.push(d);\n  }\n  if (paths.length === 0) return null;\n  // A margin keeps the pen's round caps and the ink inside the box.\n  const margin = fontSize * 0.08;\n  const viewBox = [minX - margin, minY - margin, maxX - minX + margin * 2, maxY - minY + margin * 2]\n    .map((value) => Math.round(value * 100) / 100)\n    .join(\" \");\n  return { paths, viewBox };\n}\n\ninterface TextSignatureProps extends Omit<SignatureProps, \"paths\" | \"viewBox\"> {\n  /** What to write. */\n  text: string;\n  /**\n   * URL of a TTF, OTF or WOFF font (not WOFF2, which opentype.js cannot\n   * read). Script and handwriting fonts look the part. It is fetched in the\n   * browser, so it has to be served from your site or allow CORS.\n   */\n  font: string;\n  /** Size of the lettering in viewBox units; the SVG itself scales with CSS. */\n  fontSize?: number;\n}\n\n/**\n * Writes any text in any font as a signature: the glyph outlines are drawn\n * by the pen and filled with ink behind it. The font is parsed in the\n * browser with opentype.js, loaded on demand so it only costs the pages that\n * use it. The SVG is named by the text for screen readers, and until the\n * font arrives it keeps its size and shows nothing.\n *\n * @example\n * <TextSignature text=\"Anne Bonny\" font=\"/fonts/handwriting.ttf\" className=\"h-16 w-auto\" />\n */\nfunction TextSignature({\n  text,\n  font,\n  fontSize = 72,\n  ink = true,\n  strokeWidth = 1,\n  inkWidth,\n  \"aria-label\": label,\n  ...props\n}: TextSignatureProps) {\n  const [lettered, setLettered] = useState<{\n    key: string;\n    paths: string[];\n    viewBox: string;\n  } | null>(null);\n  const key = `${font}\\u0000${fontSize}\\u0000${text}`;\n\n  useEffect(() => {\n    let cancelled = false;\n    loadFont(font)\n      .then((parsed) => {\n        const result = lettering(parsed, text, fontSize);\n        if (!cancelled) setLettered(result ? { key, ...result } : null);\n      })\n      .catch((error: unknown) => {\n        if (cancelled) return;\n        setLettered(null);\n        console.error(error);\n      });\n    return () => {\n      cancelled = true;\n    };\n  }, [font, fontSize, text, key]);\n\n  const ready = lettered?.key === key ? lettered : null;\n\n  return (\n    <Signature\n      // A new outline set is a new drawing: remount so the pen starts over.\n      key={ready?.key ?? \"blank\"}\n      paths={ready?.paths ?? []}\n      viewBox={ready?.viewBox ?? `0 0 ${fontSize * Math.max(text.length, 1) * 0.5} ${fontSize}`}\n      ink={ink}\n      inkWidth={inkWidth ?? fontSize * 0.18}\n      strokeWidth={strokeWidth}\n      aria-label={label ?? text}\n      {...props}\n    />\n  );\n}\n\nexport { TextSignature, type TextSignatureProps };\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "text"
  ],
  "type": "registry:ui"
}