How to Make a Text Box: A Crash Course in Unicode and OpenType

Lobsters Hottest News

Summary

BSE 2024 talk recap: Jimmy Lee explains from first principles how to build a truly usable multilingual text box using the Unicode and OpenType standards, emphasizing that the mapping from characters to glyphs is an irreversible, one-way data flow.

<p><a href="https://lobste.rs/s/tslwlq/how_make_text_box_crash_course_unicode">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 10/01/26, 08:31 PM

**TL;DR: Building a working text box from scratch means bridging two worlds — Unicode (characters) and OpenType (glyphs) — and the data flow between them is an irreversible, one-way pipeline: decoding, normalization, cmap mapping, and reordering marks. None of them can be skipped.** ## From a Triangle to an Interface In the spring of 2024, on the BSE stage, Jimmy Lee shared the past two years of his work: how to actually render text to the screen, rather than just drawing a rectangle. He first put the audience into a particular frame of mind: imagine you're a young, ambitious programmer who wants to write a graphical application from scratch, relying on no existing libraries, starting entirely from first principles. The first step in making a graphical application is opening a window, and how hard that is depends on the platform you choose. Next comes drawing into the window — that is, talking to the GPU, which is again highly platform-dependent. A few days later, you finally have a triangle on screen. Given a triangle, a rectangle is easy; and given a rectangle, you can start thinking about buttons, checkboxes, sliders, and hooking up mouse interaction. But if you screenshot such an interface and send it to someone, their first question is often: "What does this actually do?" — because the interface has no text anywhere on it. ## From Bounding Boxes to Advance to Kerning Starting from first principles, consider English text. Stare at it for a few seconds and you'll see that it's nothing more than a bunch of letters and symbols, each of which seems like it could be stuffed into a rectangle. Since we can draw rectangles now, assume each symbol has a bounding box and draw a rectangle around every glyph. Once you've drawn the bounding boxes, you'll notice there are still gaps between the symbols. As we draw characters from left to right, we actually want the pen to advance by more than the glyph's visual width. In typography, this is called the **advance**. So to draw an English string, we need each symbol's width and advance. With those two things in hand, the basic code is actually quite simple: iterate over the string, map each character to a glyph, draw it at the exact cursor position, then advance the cursor by that glyph's advance. If you think that doesn't look good enough, you can add **kerning** — in the pseudocode-style example, that requires changing just three lines. Kerning addresses cases like this: the letter T has a fairly wide bounding box because its top bar is wide, but we know it's only the top bar that's wide, and smaller letters can comfortably fit underneath it. So we need a way to check each pair of adjacent glyphs as we iterate over the string and make an adjustment before drawing, bringing the T and the following lowercase letter closer together. The speaker's conclusion was blunt: if you're drawing simple English text, advance plus kerning is essentially everything you need. ## TrueType: A Ready-Made Source of Data The problem is that most programmers aren't typographers or font designers — where does the data needed for drawing come from? Fortunately, there's a widely used, widely supported format that implements exactly what was described above: **TrueType**. Introduced by Apple in the late 1980s and later licensed to Microsoft, TrueType contains: - **The cmap table**: mapping characters to glyphs; - **Vector outlines**: not covered in this talk; - **Kerning information**: give it a pair of glyphs, it returns an adjustment value. Once the sample code was adapted to use TrueType fonts, simple English text appeared, and the interface started to look like something real. ## Once a Text Box Is Introduced, the Real Trouble Begins The next step is adding a text input field. At this point the program faces two unsolved problems: 1. You're dealing with fully **dynamic strings** — the person deciding what goes into the text box isn't you; 2. You want it to work correctly even when the user isn't typing in English. They might want to type in an alphabet different from yours, such as Greek or Cyrillic; they might want to type in Chinese, Japanese, or Korean; they might want to type in Arabic or Hebrew — scripts that write in directions different from English — and they might want to type in any Brahmi-derived script, which covers India and its surroundings as well as most of Southeast Asia. Ideally, it should work for any language on Earth. In the speaker's own words: "Come on, we're trying to build good software." That's precisely the central question of this talk: how on earth does this work? ## The Division of Labor: Unicode and OpenType The speaker was explicit about his goal: to make people familiar with each step of going from a multilingual font system to actually rendering things on screen and presenting them correctly to the user, covering a whole set of Unicode and OpenType processing workflows. But he also drew clear boundaries — he **would not** explain how to implement your own Unicode/OpenType library (that would make the talk much longer and consist largely of trivia), he **would not** explain how to render glyphs (there are experts in the audience far better than him at that), and he **would not** explain how to handle IMEs (Input Method Editors — the complex input methods used for certain languages). ### What Is Unicode? Technically, Unicode is a very large standard, currently at **version 17**, and still being updated by the Unicode Consortium. It covers many different concepts, all under the Unicode umbrella: - Which integers correspond to which characters — **code points**; - How those integers are represented in memory — the **UTF series of text encodings**; - A collection of tables storing the properties of every character in Unicode space — the **Unicode Character Database (UCD)**; - A complete set of **algorithms** for processing Unicode strings. ### What Is OpenType? OpenType's focus is much narrower. It's essentially the "query language" or extended version of TrueType mentioned earlier, introduced by Microsoft in the late 1990s, defining the file format as well as all the processes needed to work with that format. It's a **strict superset** of TrueType. The file format itself is very well documented, but the actual behavior of fonts is much less so. ### Characters and Glyphs There are two fundamentally different worlds here. Unicode primarily deals with **characters**, and characters are best understood as **elements of user input** — even though intuitively we tend to think of them as linguistic elements. The reason "user input" is emphasized is that the Unicode character set contains a large number of characters that exist purely for formatting or controlling specific algorithms and carry no linguistic meaning whatsoever. For that reason — and because this perspective generalizes well for programmers — it's convenient to imagine a Unicode string as a sequence of **user instructions**. These characters are represented by code points, which are **20-bit integers**. OpenType, on the other hand, deals with **glyphs**. Glyphs are essentially just visual information: a bounding box, and inside it a bunch of glyph outlines. Glyphs are referenced by **glyph indices**, which are **16-bit unsigned integers**, and how they're assigned is entirely up to the font itself. The key conclusion: there is a way to map a Unicode string into a sequence of OpenType glyphs, but **once that conversion is done, you can't go back — this is an explicit one-way data flow**. Once you're in OpenType space, you lose a great deal of information. ## The Steps Along This One-Way Data Flow Following the order of the transformations, the speaker began walking through them one by one. ### Step 1: Decoding The "UTF" here can be any text encoding; in essence it's about how the 20-bit integers defined by Unicode are represented in memory in some way. This transformation is essentially arbitrary. The speaker mentioned hearing someone say: "Oh, text could have been much simpler if it were just UTF-32" — that is, one 32-bit integer holding one code point. But when you do that, all the code you save amounts to very little, while the memory footprint quadruples, or more realistically increases two to threefold. Using UTF-32 doesn't save you much complexity at all. After decoding, we have code points. ### Step 2: From Code Points to Glyphs — cmap and Normalization Recall TrueType: it works via the **cmap table**, which is a one-to-one mapping. This is still a one-to-one mapping, but only if **your character set contains no ambiguity** — that is, whatever you type, there is only one way for the text encoding or character set to encode what appears on screen. Unfortunately, Unicode isn't like that. A very simple example: to write the letter E with an acute accent, you have two options: 1. Use one code point: Latin Capital Letter E with Acute; 2. Use two code points: the letter E, followed by a combining acute accent. So how do you reconcile this property of Unicode with the one-to-one mapping that the cmap table requires? The speaker offered a hint: **the cmap table still exists in OpenType, and it's still the only way to perform this mapping.** Fortunately, the Unicode standard provides a set of tools for this — **Unicode normalization forms** (Unicode Annex, UAX). Normalization describes exactly what it takes to eliminate ambiguity: it primarily describes two forms, one preferring "E plus diacritic" and the other preferring "a single accented E code point." Back in OpenType, the ideal form of the mapping would be: take the code points, normalize the string, and hand it to cmap, which expects its input to have been normalized in some way — this way the one-to-one mapping still holds and everyone gets their glyph indices. Here's the obvious problem: there are two main normalization forms — which one does OpenType use? Is it written into the standard, or is it just a convention? The answer is: **neither**. What actually happens is that OpenType libraries need to **try every possible combination of code points** to see which ones match the cmap table. The speaker closed this topic with a bit of self-deprecation: "Don't laugh — this has been my life for the past two years." ### Step 3: Reordering Marks After trying all these combinations, the Unicode normalization process involves several other steps. These steps are taken one by one from the standard, so the Unicode algorithms can truly be applied — for instance, **reordering marks**. Reordering marks handle another kind of ambiguity: since combining characters can be stacked indefinitely, what exactly should you do if you only want to write a particular letter with an acute accent? The transcript breaks off here; the second half of the talk (and the Q&A session) was not recorded. ## Summary The through-line of Jimmy Lee's talk is this: a seemingly simple text box rests on the cooperation between two standards — Unicode and OpenType. Unicode handles characters (user input, 20-bit code points, a great deal of ambiguity); OpenType handles glyphs (visual information, 16-bit indices); and the conversion between them is an irreversible one-way data flow. The real workload isn't in rendering, nor in the font format itself — it's in normalization, reordering, composition, and the whole series of Unicode algorithms. And there's a fact that leaves implementers rather resigned: the cmap table's one-to-one mapping can only be "approximated" by brute-forcing every possible combination of code points. > Note: The original transcript breaks off at the "Reordering Marks" section; this article only covers the content that was provided. Source: How to make a text box: A crash course in Unicode and OpenType (https://www.youtube.com/watch?v=7Tr0ty9-yeQ) (discussion thread on Lobsters (https://lobste.rs/s/tslwlq/how_make_text_box_crash_course_unicode))

Similar Articles

@0xCheshire: A photo of handwritten letters instantly becomes a custom font that can be installed on your computer or used on websites. This project is called draw-your-font. Write letters on paper with a dark pen, take a photo, drag it into the browser, and generate a TTF; when used with Claude Code Skill, it can also automatically recognize...

X AI KOLs Timeline

draw-your-font is an open-source project that converts photos of handwritten letters into installable fonts (TTF, WOFF) using local AI processing, enabling personal or commercial use without uploading images.

@GoJun315: A developer on Reddit today shared that videos played in a web page can be rendered purely with text characters. The open-source project used is ASCILINE, a real-time ASCII video rendering engine. It supports two rendering modes: - ASCII mode: restores the image with ordinary characters by brightness and color, you can see the characters flowing...

X AI KOLs Timeline

ASCILINE is a high-performance open-source real-time ASCII video rendering engine that converts videos into plain text character displays, supporting multiple rendering modes, low-bandwidth transmission, and CSS effects overlay.

all the ways in which terminals' text rendering is bad (2024)

Lobsters Hottest

The article explores various fundamental problems with text rendering in terminal emulators, including character definition ambiguity, Unicode handling issues, flawed 2D grid assumptions, and cursor desyncs, highlighting the difficulty of supporting complex scripts and fonts.

Unicode Variation Selector-15 and some of my tears

Lobsters Hottest

A developer investigates why footnote backlink glyphs render as emoji in RSS readers, explores Unicode variation selectors (U+FE0E and U+FE0F) for controlling text vs emoji presentation, and discusses the broader challenges of variation selectors for web content.

TextVerifier: Seeing Beyond Recognition to Verify Chinese Text Rendering

Hugging Face Daily Papers

TextVerifier 提出了一种针对中文文本渲染缺陷的逐字符验证方法,利用表意文字描述序列(IDS)作为文本塔输入,并在 TextRenderBench-ZH 基准上达到 0.844 的 F1,显著优于 OCR 置信度、通用 VLM 和 TextPecker,还可作为 Flow-GRPO 后训练的逐字符奖励进一步降低渲染错误。