{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "persian-reshape",
  "title": "Persian Reshape",
  "description": "Pre-shapes Persian/Arabic text into joined Unicode presentation-form codepoints so it renders correctly in Satori/next-og images, which don't shape connected scripts on their own.",
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/base/lib/persian-reshape.ts",
      "content": "/**\n * Renderers that don't perform text shaping themselves — most notably\n * Satori, which powers Next's `ImageResponse` (`next/og`) and therefore\n * every OG-image route — draw each character as its isolated glyph and\n * never join Arabic/Persian letters into connected script. Persian text\n * handed to them renders as disconnected letters instead of the joined\n * script readers expect.\n *\n * `reshapePersian` maps each letter to the correct\n * isolated/initial/medial/final Unicode Arabic Presentation Forms\n * codepoint based on its neighbors, and reorders the result for renderers\n * (Satori included) that draw characters in raw logical order without\n * applying the Unicode Bidi Algorithm — so the output already looks\n * correct with zero runtime shaping required.\n *\n * Presentation-form codepoints and the letter-joining table are adapted\n * from the public domain Arabic/Persian shaping map popularized by\n * node-arabic-persian-reshaper (Shen Yiming, MIT), itself forked from\n * font-store/persian-reshaper and originally based on Accorpa's\n * Arabic-Converter-From-and-To-Arabic-Presentation-Forms-B. Codepoints are\n * written as \\u escapes (rather than pasted glyphs) so the mapping stays\n * auditable against the Unicode Arabic Presentation Forms-A (U+FB50-FDFF)\n * and Forms-B (U+FE70-FEFF) blocks.\n */\n\ninterface LetterForms {\n  isolated: string\n  final: string\n  initial: string\n  medial: string\n  /** Whether this letter can pass a connection on to the next letter. */\n  dual: boolean\n}\n\nfunction forms(\n  isolated: number,\n  final: number,\n  initial?: number,\n  medial?: number\n): LetterForms {\n  return {\n    isolated: String.fromCharCode(isolated),\n    final: String.fromCharCode(final),\n    initial: String.fromCharCode(initial ?? isolated),\n    medial: String.fromCharCode(medial ?? final),\n    dual: initial !== undefined,\n  }\n}\n\nconst LETTERS: Record<string, LetterForms> = {\n  // Hamza — isolated only, never joins.\n  ء: forms(0xfe80, 0xfe80),\n  // Non-connecting (right-joining only) letters — isolated/final only.\n  آ: forms(0xfe81, 0xfe82), // آ alef madda\n  أ: forms(0xfe83, 0xfe84), // أ alef hamza above\n  ؤ: forms(0xfe85, 0xfe86), // ؤ waw hamza\n  إ: forms(0xfe87, 0xfe88), // إ alef hamza below\n  ا: forms(0xfe8d, 0xfe8e), // ا alef\n  د: forms(0xfea9, 0xfeaa), // د dal\n  ذ: forms(0xfeab, 0xfeac), // ذ thal\n  ر: forms(0xfead, 0xfeae), // ر reh\n  ز: forms(0xfeaf, 0xfeb0), // ز zain\n  ژ: forms(0xfb8a, 0xfb8b), // ژ jeh (Persian)\n  و: forms(0xfeed, 0xfeee), // و waw\n  ى: forms(0xfeef, 0xfef0), // ى alef maksura\n  ة: forms(0xfe93, 0xfe94), // ة teh marbuta\n  // Tatweel — an elongation stroke, not a letter; passes through unchanged\n  // and always \"connects\" so it never breaks a run.\n  ـ: forms(0x0640, 0x0640, 0x0640, 0x0640),\n\n  // Dual-joining letters — isolated, final, initial, medial.\n  ب: forms(0xfe8f, 0xfe90, 0xfe91, 0xfe92), // ب beh\n  پ: forms(0xfb56, 0xfb57, 0xfb58, 0xfb59), // پ peh (Persian)\n  ت: forms(0xfe95, 0xfe96, 0xfe97, 0xfe98), // ت teh\n  ث: forms(0xfe99, 0xfe9a, 0xfe9b, 0xfe9c), // ث theh\n  ج: forms(0xfe9d, 0xfe9e, 0xfe9f, 0xfea0), // ج jeem\n  چ: forms(0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d), // چ tcheh (Persian)\n  ح: forms(0xfea1, 0xfea2, 0xfea3, 0xfea4), // ح hah\n  خ: forms(0xfea5, 0xfea6, 0xfea7, 0xfea8), // خ khah\n  س: forms(0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4), // س seen\n  ش: forms(0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8), // ش sheen\n  ص: forms(0xfeb9, 0xfeba, 0xfebb, 0xfebc), // ص sad\n  ض: forms(0xfebd, 0xfebe, 0xfebf, 0xfec0), // ض dad\n  ط: forms(0xfec1, 0xfec2, 0xfec3, 0xfec4), // ط tah\n  ظ: forms(0xfec5, 0xfec6, 0xfec7, 0xfec8), // ظ zah\n  ع: forms(0xfec9, 0xfeca, 0xfecb, 0xfecc), // ع ain\n  غ: forms(0xfecd, 0xfece, 0xfecf, 0xfed0), // غ ghain\n  ف: forms(0xfed1, 0xfed2, 0xfed3, 0xfed4), // ف feh\n  ق: forms(0xfed5, 0xfed6, 0xfed7, 0xfed8), // ق qaf\n  ك: forms(0xfed9, 0xfeda, 0xfedb, 0xfedc), // ك kaf (Arabic)\n  ک: forms(0xfb8e, 0xfb8f, 0xfb90, 0xfb91), // ک keheh (Persian) — dedicated forms, distinct from Arabic kaf\n  گ: forms(0xfb92, 0xfb93, 0xfb94, 0xfb95), // گ gaf (Persian)\n  ل: forms(0xfedd, 0xfede, 0xfedf, 0xfee0), // ل lam\n  م: forms(0xfee1, 0xfee2, 0xfee3, 0xfee4), // م meem\n  ن: forms(0xfee5, 0xfee6, 0xfee7, 0xfee8), // ن noon\n  ه: forms(0xfee9, 0xfeea, 0xfeeb, 0xfeec), // ه heh\n  ي: forms(0xfef1, 0xfef2, 0xfef3, 0xfef4), // ي yeh (Arabic)\n  ی: forms(0xfbfc, 0xfbfd, 0xfbfe, 0xfbff), // ی farsi yeh (Persian) — dedicated forms, distinct from Arabic yeh\n  ئ: forms(0xfe89, 0xfe8a, 0xfe8b, 0xfe8c), // ئ yeh hamza\n}\n\n/** Lam followed by one of these alef variants merges into one ligature glyph. */\nconst LAM_ALEF_LIGATURES: Record<string, { isolated: string; final: string }> =\n  {\n    ا: { isolated: \"ﻻ\", final: \"ﻼ\" }, // لا\n    آ: { isolated: \"ﻵ\", final: \"ﻶ\" }, // لآ\n    أ: { isolated: \"ﻷ\", final: \"ﻸ\" }, // لأ\n    إ: { isolated: \"ﻹ\", final: \"ﻺ\" }, // لإ\n  }\n\nconst LAM = \"ل\"\n\n/**\n * Arabic combining diacritics (harakat/tashkeel) are \"transparent\" to\n * shaping: their presence between two letters doesn't break the joining\n * between them, and they aren't shaped themselves. They pass straight\n * through in the output at their original position.\n */\nconst TRANSPARENT = new Set(\n  [\n    0x0610, 0x0611, 0x0612, 0x0613, 0x0614, 0x0615, 0x064b, 0x064c, 0x064d,\n    0x064e, 0x064f, 0x0650, 0x0651, 0x0652, 0x0653, 0x0654, 0x0655, 0x0656,\n    0x0657, 0x0658, 0x0670, 0x06d6, 0x06d7, 0x06d8, 0x06d9, 0x06da, 0x06db,\n    0x06dc, 0x06df, 0x06e0, 0x06e1, 0x06e2, 0x06e3, 0x06e4, 0x06e7, 0x06e8,\n    0x06ea, 0x06eb, 0x06ec, 0x06ed,\n  ].map((code) => String.fromCharCode(code))\n)\n\n/** Nearest non-transparent letter looking in `step` direction from `from`. */\nfunction nearestLetter(\n  chars: string[],\n  from: number,\n  step: 1 | -1\n): string | undefined {\n  let i = from\n  while (i >= 0 && i < chars.length) {\n    const ch = chars[i] as string\n    if (!TRANSPARENT.has(ch)) return ch\n    i += step\n  }\n  return undefined\n}\n\n/**\n * Reshapes Persian/Arabic text into pre-joined Unicode presentation-form\n * codepoints so it renders correctly through renderers that don't shape\n * connected scripts themselves — chiefly Satori (`next/og`), the target\n * this was built for.\n *\n * @example\n * reshapePersian(\"راهنمای خرید\") // renders as connected script in an <ImageResponse>\n */\nexport function reshapePersian(text: string): string {\n  const chars = Array.from(text)\n\n  // Pass 1: shape each letter (or lam-alef ligature) using its LOGICAL\n  // neighbors — skipping over transparent diacritics when looking for the\n  // nearest real letter, since they don't break a joining run.\n  const tokens: { glyph: string; rtl: boolean }[] = []\n  for (let i = 0; i < chars.length; i++) {\n    const ch = chars[i] as string\n\n    if (TRANSPARENT.has(ch)) {\n      tokens.push({ glyph: ch, rtl: true })\n      continue\n    }\n\n    const prev = nearestLetter(chars, i - 1, -1)\n    const next = nearestLetter(chars, i + 1, 1)\n\n    if (ch === LAM && next && LAM_ALEF_LIGATURES[next]) {\n      const ligature = LAM_ALEF_LIGATURES[next]!\n      const hasIncoming = !!prev && !!LETTERS[prev] && LETTERS[prev]!.dual\n      tokens.push({\n        glyph: hasIncoming ? ligature.final : ligature.isolated,\n        rtl: true,\n      })\n      // Consume the alef too — it may itself be preceded by transparent\n      // marks, so skip forward past however many chars nearestLetter used.\n      let j = i + 1\n      while (chars[j] !== next) j++\n      i = j\n      continue\n    }\n\n    const letterForms = LETTERS[ch]\n    if (!letterForms) {\n      tokens.push({ glyph: ch, rtl: false })\n      continue\n    }\n\n    const hasIncoming = !!prev && !!LETTERS[prev] && LETTERS[prev]!.dual\n    const hasOutgoing = letterForms.dual && !!next && !!LETTERS[next]\n\n    let glyph: string\n    if (hasIncoming && hasOutgoing) glyph = letterForms.medial\n    else if (hasIncoming) glyph = letterForms.final\n    else if (hasOutgoing) glyph = letterForms.initial\n    else glyph = letterForms.isolated\n\n    tokens.push({ glyph, rtl: true })\n  }\n\n  // Pass 2: Satori draws characters in raw array order without applying\n  // the Unicode Bidi Algorithm itself, so an RTL string shaped in logical\n  // (typed) order still comes out visually backwards. Group into runs of\n  // same-direction tokens, reverse the run order (the paragraph is\n  // predominantly RTL) and reverse token order *within* each RTL run only\n  // — a neutral run (spaces, digits, embedded Latin) keeps its own\n  // internal order so mixed content doesn't get mangled.\n  const runs: { glyphs: string[]; rtl: boolean }[] = []\n  for (const token of tokens) {\n    const lastRun = runs[runs.length - 1]\n    if (lastRun && lastRun.rtl === token.rtl) {\n      lastRun.glyphs.push(token.glyph)\n    } else {\n      runs.push({ glyphs: [token.glyph], rtl: token.rtl })\n    }\n  }\n\n  return runs\n    .reverse()\n    .map((run) => (run.rtl ? [...run.glyphs].reverse() : run.glyphs).join(\"\"))\n    .join(\"\")\n}\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}