How I generate 251 cursive text styles with plain Unicode (no fonts, no images) published: false

2026-11-29来源 dev.toEnglish
How I generate 251 cursive text styles with plain Unicode (no fonts, no images) published: false

"Cursive text" you paste into Instagram or a game nickname is not a font. It is ordinary text made of different Unicode characters. I run a small free tool site, and here is how the generator works and what bit me.

The idea

Unicode has a block called Mathematical Alphanumeric Symbols with complete alphabets in styles like script, bold script, fraktur and double-struck. Each style is a continuous range, so converting text is just arithmetic on code points:

const BOLD_SCRIPT_UPPER = 0x1D4D0; const BOLD_SCRIPT_LOWER = 0x1D4EA; function boldScript(text) { return [...text].map(ch => { const c = ch.codePointAt(0); if (c >= 65 && c <= 90) return String.fromCodePoint(BOLD_SCRIPT_UPPER + c - 65); if (c >= 97 && c <= 122) return String.fromCodePoint(BOLD_SCRIPT_LOWER + c - 97); return ch; // keep spaces, digits, punctuation }).join(''); } boldScript("Minha Letra"); // 𝓜𝓲𝓷𝓱𝓪 𝓛𝓮𝓽𝓻𝓪 

Spread the string ([...text]) instead of indexing it, because the output characters live outside the BMP and take two UTF-16 units.

The gotcha: the "light" script has holes

The thin script alphabet is not continuous. Letters like B, E, F, H, I, L, M, R and lowercase e, g, o live in a different block (Letterlike Symbols), so you need a small exception table:

const SCRIPT_EXCEPTIONS = { B: 0x212C, E: 0x2130, F: 0x2131, H: 0x210B, I: 0x2110, L: 0x2112, M: 0x2133, R: 0x211B, e: 0x212F, g: 0x210A, o: 0x2134 }; 

Forget this and some letters turn into empty boxes.

Getting from 5 styles to 251

Instead of writing 251 functions, I combine a few alphabets (bold script, light script) with a list of decorations that wrap the text, such as small symbols before and after. The decoration list is just data, so adding a style costs one line.

Keeping the page light

251 result cards means a lot of DOM. The page renders the first 30 and a "show more" button renders the rest, so a cheap phone isn't building thousands of elements on load.

A layout shift I didn't expect

My heading uses a custom font. When it loaded late, the title changed size and pushed the tool down: a layout shift of 0.373 on one page in my local test. Preloading that one font file brought it to 0:

<link rel="preload" href="/webfonts/my-heading.woff" as="font" type="font/woff" crossorigin> 

Limits you should know

  • Screen readers read these characters badly, so don't use them for important text.
  • The text isn't searchable as the original word.
  • Some older phones lack glyphs for a few characters and show empty boxes, so test on a real device.

If you want to try it, the tool is here: Letras Cursivas para Copiar e Colar (the interface is in Portuguese, the idea works for any language that uses Latin letters). I'd like to hear how you'd handle accents, which these ranges don't cover.

原文:https://www.a8q9.cn/en/article/how-i-generate-251-cursive-text-styles-with-plain-unicode-no-fonts-no-images-pub-8511f8.html

相关文章