DEV Community

MD SAIFUL ISLAM
MD SAIFUL ISLAM

Posted on Originally published at texttools.pro

Why Legacy Bangla Text Breaks on the Web: Building a Client-Side Unicode ↔ ANSI Transcoder in JavaScript

If you have ever built a web app for users in South Asia, you have probably seen pasted Bengali text turn into unreadable mojibake like Avgvi †mvbvi evsjv instead of আমার সোনার বাংলা.

This happens because millions of newspaper archives, government notices, and print-house templates still rely on ANSI-mapped legacy fonts (most notably SutonnyMJ / Bijoy), whereas modern browsers, smartphones, and APIs use Unicode.

A simple String.replace() lookup table is not enough to convert between the two. In this post, we will break down the exact linguistic and byte-level differences—and build a clean, zero-server JavaScript transposition pass that handles complex scripts properly.


1. Logical Order (Unicode) vs. Visual Order (ANSI)

In Unicode, characters are stored in logical phonetic order—the way you speak and type them:

  1. Consonant (ক = U+0995)
  2. Dependent vowel sign / Kar (ি = U+09BF)

Even though ি (ikar) visually renders to the left of ক (কি), Unicode stores ক first and ি second. The OS text-shaping engine (HarfBuzz / DirectWrite) handles visual placement automatically.

Legacy ANSI fonts have no shaping engine. Instead, they hijack standard Latin ASCII code points (0x20–0xFF) and store glyphs in visual left-to-right order. So কি is stored physically as w (ি) followed by K (ক) → wK.

The same visual-order rule applies to:

  • ি (w), ে († / ‡), and ৈ (ˆ / ‰) — which appear before the consonant they modify.
  • Split two-part vowels like ো (ে + া) and ৌ (ে + ৗ), which wrap around the consonant (†Kv for কো).

2. The Pre-Base Reordering Algorithm in JavaScript

To convert ANSI → Unicode, your parser must:

  1. Map multi-character ANSI ligatures first (longest-match-first, e.g., 'Av' → 'আ', '¯^' → 'স্ব') before single-character keys.
  2. Detect pre-base vowel signs (ি, ে, ৈ) and move them after the immediately following consonant cluster (including any hasanta ্ conjuncts).

Here is a simplified, working JavaScript reordering pass demonstrating how pre-base signs and Ref (র্ / ©) are normalized:

const PRE_BASE_VOWELS = new Set(['ি', 'ে', 'ৈ']);

function reorderToLogicalUnicode(rawMappedText) {
  const chars = [...rawMappedText];
  const result = [];

  for (let i = 0; i < chars.length; i++) {
    const ch = chars[i];

    // 1. Pre-base vowel sign must jump AFTER the next consonant (or conjunct)
    if (PRE_BASE_VOWELS.has(ch) && i + 1 < chars.length) {
      let j = i + 1;
      result.push(chars[j]);

      // Keep advancing across conjuncts joined by Hasanta (্)
      while (j + 2 < chars.length && chars[j + 1] === '্') {
        result.push('্', chars[j + 2]);
        j += 2;
      }

      result.push(ch);
      i = j;
      continue;
    }

    result.push(ch);
  }

  return result.join('');
}
Enter fullscreen mode Exit fullscreen mode

3. Handling Ref (র্) and Split Vowels (ো, ৌ)

Two edge cases trip up almost every naive converter:

  • Split Vowels (ে + consonant + া): In ANSI, কো is written as †Kv (ে + ক + া). After ে jumps past ক to produce কো, a normalization pass merges ে + া into the single Unicode code point ো (U+09CB) and ে + ৗ into ৌ (U+09CC).
  • Ref (র্): In Unicode, র্ (র + ্) comes before the base consonant (কর্ম = ক + র + ্ + ম), whereas in ANSI © sits after the consonant (Kg©). Reversing that transposition is essential so words like সূর্য or ধর্ম don't break.

4. Why Client-Side Processing Matters

Because the entire state machine and conjunct lookup table (~250+ ligatures) weigh only a few kilobytes of JavaScript, there is no reason to send user documents to a backend server. Running the transcoder directly in browser memory gives sub-millisecond output while keeping confidential drafts, legal notices, and manuscripts private.

I recently built a full client-side implementation of this engine (along with automatic encoding detection for mixed Unicode/Bijoy text) as part of an open web utility suite: Bangla Unicode ↔ Bijoy Converter on TextTools.pro.

(Disclosure: I used AI assistance for drafting and refining code examples in this tutorial.)

What complex-script rendering or encoding edge cases have you run into in JavaScript? Let me know in the comments!

Top comments (0)