All work

Tiqian / Cross-Platform CJK Layout Engine

Printed CJK books follow careful typesetting rules for line breaks, punctuation spacing, justification and ruby annotations, and a browser follows few of them. Tiqian exists to bring CJK text on the web, Android and desktop up to the standard of a printed page. Duo123 designed its typography rules and began the engine in June 2026. I joined in August and took on the work between those rules and a reader’s screen: the browser scheduler, a Rust build path, and boring, the compiler that now generates the engine for five languages from one source. One downstream site’s build fell from 25 minutes to under 5.

Tiqian output: horizontal Mandarin textruby · emphasis marks · punctuation squeeze
A Mandarin paragraph set by Tiqian, with pinyin ruby over one character, emphasis dots under a phrase, and squeezed punctuation

try the rules yourself

Browsers give every CJK punctuation mark a full character cell, and adjacent marks leave visible gaps.

A comma or a closing bracket takes a whole character cell, with the mark drawn in one half. When two marks meet, their empty halves add up to a visible gap. Latin words and numbers sit at whatever distance the author typed, and lines with mixed scripts end short of the right edge.

The W3C’s CLREQ layout requirements describe how a printed book handles each case. Duo123 had turned those rules into a Kotlin engine that measures and places every glyph. The rules below are three of them.

Rules

他强调:(豆子新鲜最要紧,烘焙其次。)这家店开在2019 年,只用Ethiopia Guji 的豆子。《咖啡手册》、《风味轮》都说:espresso要在 25 秒内萃取完。

Turn the rules on one at a time. This is a CSS re-creation of three CLREQ rules for this page. Tiqian positions every glyph itself and implements many more.

The same blog paragraph typeset by Tiqian: adjacent punctuation squeezed and both edges flushA blog paragraph typeset by the browser: gaps between adjacent punctuation and a ragged right edgeBrowserTiqian

Browser

Each punctuation mark keeps its full cell, so “)。” in the third paragraph has a visible gap, and lines with Latin words end short of the right edge.

Tiqian

The same paragraph on a real blog post, after the reader’s page loads Tiqian. Drag the line, or use the arrow keys.

my first week on the project

Resizing the window froze the tab, because two schedulers ordered the same frame independently.

In August I put the first web build into a real reading page. Text looked right. Dragging the window edge stalled the tab, and paragraphs flickered back to the browser’s layout mid-drag.

The engine’s layout jobs scheduled their own animation frames. The page’s coordinator dispatched work on its own rhythm. Each side had its own answer to which work ran first in a frame and when a width counted as stable. I gave every decision to the coordinator: each frame it hands out bounded slices of time, and the engine decides how to use each slice.

Layout gets a slice of each frame

The slice is 40% of the measured frame interval, kept between 2.5 ms and 6 ms. The browser keeps the rest for style, paint and input.

16.7 msbetween frames at 60 Hz
6.0 msfor typesetting
10.7 msleft to the browser

budget = clamp(frame interval × 0.4, 2.5 ms, 6 ms)

The viewport is typeset first

One coordinator now grants every slice. Paragraphs in view go first, then the ones just outside it. Off-screen paragraphs wait until their own width has stopped changing for 200 ms, so during a drag the frame budget goes to the text on screen.

In viewTypeset first.Near the viewportTypeset next.Far awayAfter the width has rested for 200 ms.

Schematic timing.

A time estimate that blocked every grant

The first coordinator predicted how long a slice would take and skipped grants that looked too long. One slow slice at cold start pushed the shared estimate past the budget, and the page fell to one paragraph every three frames. I removed the prediction, and the coordinator now decides from elapsed time alone.

Layout reads with no consumer

A profile of a fast drag showed 23% of the time in one layout read, taken for every paragraph whenever a job finished. While the width was moving, no code used the result. Skipping it cut those reads in a stress test from about 615 to 335–419.

with an equivalence audit

Sites waited minutes for every build, so the engine moved into the build in Rust.

Static sites lay out each article ahead of time, so readers get finished text in the HTML. The build did that through the engine compiled to JavaScript, plus two WebAssembly packages for fonts. On a 285-page site, layout took most of a four-minute build.

Kotlin/Native compiles the engine to a static library with a C interface. To call it from Node.js build tools, I wrote a Rust layer around it: a Neon addon that runs paragraphs on worker threads, a font session in Rust, and a cache keyed by each paragraph’s content hash, so unchanged chapters skip layout on the next build.

−0%

Downstream CI prerendering, from 25 minutes to 4 minutes 45 seconds.

Before claiming it

I checked the output matched.

A faster build that sets one line differently is a regression. Each benchmark came with an equivalence audit, layer by layer.

SvelteKit reference site285 pages, 6,519 paragraphs

Kotlin/JS engine
263.3 s
Rust addon, empty cache
55.0 s
Rust addon, cache hits
14.3 s

Astro reference site326 paragraphs

Kotlin/JS engine
137.3 s
Rust addon, empty cache
29.3 s
Rust addon, cache hits
9.2 s
End-to-end build time on one laptop (Ryzen 7 8845HS), starting from an empty layout cache unless marked.

Same requests

Every paragraph request the build sent to the engine was identical, byte for byte, on both paths.

Same lines

No line broke in a different place on either site. Positions differed by at most 0.00073 px, from Kotlin/Native’s 32-bit floats.

Same output on every thread count

Builds on one, two and four threads wrote the same 306 cache entries, byte for byte.

I measured it before I fixed it

Every fix had to land in three languages, so the engine moved to a single Haxe source.

The Rust path left the project with the same logic in Kotlin, TypeScript and Rust, kept in step by parity tests. Six days later I counted what that cost. A single behavior change could touch five files.

Commit 96ef18e3 · August 2026

Replaying one font feature meant five edits in three languages.

Behavior: replay the fwid feature
  • JS Browser loweringprepared-dom.js
  • CSS Stylesheetstyles.css
  • Rust Build-time portprepared_dom.rs
  • JS Corpus builderbuild-prepared-dom-corpus.mjs
  • JSON Shared fixtureprepared-dom-corpus.fixture.json

After the move to Haxe

The same behavior is written once in Haxe and generated for every target.

engine-haxe/src
  • Kotlin kotlin-f32 · kotlin-f64
  • TS ts
  • Rust engine-rust-f32 · engine-rust-f64
  • Swift swift-f32 · swift-f64
  • Dart dart

Almost a third of the work on the Rust copy went into making it agree with the other two.

In its first six days the Rust copy got 59 commits. 17 of them added no new behavior and only fixed places where Rust, Kotlin and TypeScript gave different answers. A single source removes that work.

  • Made the copies agree again
  • Other work

Write the engine once, in the part of Haxe every target can express

Haxe compiles to many languages, and the Reflaxe framework lets you write your own output targets. No maintained target existed for Kotlin, and the Rust one shipped a GPL runtime. So boring has its own TypeScript, Kotlin and Rust emitters on Reflaxe, and later Swift and Dart. Its subset started as what Haxe, Kotlin, Rust and TypeScript share. CI rejects anything outside it.

Kotlin came first because the production engine was Kotlin. Its generated code had to match the hand-written engine’s golden output with zero difference, and every other target is measured against Kotlin.

When a ruling restricts a general operation to a subdomain, the same specification, in the same change, names the sanctioned path for the general case.From boring’s design principles: each restricted construct comes with a replacement.

The browser build skips WebAssembly.

Edge’s enhanced security mode turns off the JIT. In that mode the WebAssembly build laid out the same paragraphs in more than twice the time of the JavaScript build, with identical output. The browser release now ships JavaScript only.

Median time in three runs to lay out the same 24 paragraphs
Kotlin/JS
307.6 ms
Kotlin/Wasm
683.2 ms

five targets, one answer per question

Every compiler decision has one owner, so five targets cannot answer the same question five ways.

Each emitter had grown its own answers to shared questions: whether a value can be null, whether a copy is independent, how often an operand runs. Some answers came from lists of type names or from reading text the emitter had already printed. A fix in one target left the other four answering differently.

I reorganized boring around policies. A policy is a decision over explicit facts that returns a typed result. Each kind of decision belongs to one of seven layers, and a layer may only use the layers above it.

A failure is fixed in the layer that made the wrong decision.

Pick a failure to see which layer owns its fix.

Question that finds the owner
Where was the storage type decided, and did the read receive that decision?
Owner
Layer 3, Representation
Fix
Storage is decided once and recorded in a plan. Declarations, assignments and reads all consume that plan.

The seven layers, each using only the ones above it

  1. Source rules

    Which Haxe forms are accepted, what they mean, and how they are rewritten into a normal form.

  2. Semantic facts

    Which declaration a name means, whether a value can be null, what an operation changes, and how branches join.

  3. Representation

    How each target stores a value: optional or not, shared or copied, which number width, which error type.

  4. Operations and boundaries

    Which conversions a call needs, which places can be written, and in what order operands run.

  5. Target construction

    The target program as a structure of values, statements, exits and temporaries.

  6. Printing

    Text, precedence, escaping, and source positions for diagnostics.

  7. Platform

    Imports, runtime helpers, extern signatures and the files the package ships.

The four failures come from the examples table in boring’s problem-analysis guide.

Three rules keep each fact in one place.

  1. Each fact has one producer.

    A fact such as “this value can be null” has one module that computes it, a scope, its readers, and the writes that invalidate it.

  2. Asking a policy changes nothing.

    A policy query does not render code, allocate a temporary name, or change flow state. Construction consumes its answer and reports what it produced.

  3. Printing never reads its own output.

    The printer turns a finished structure into text. It cannot recover a fact by parsing code it has already printed.

The migration was split into six packages that agents could work on in parallel.

Each package owns one kind of decision across all five targets. Before any code changed, its brief named the source rules, the facts it reads and who produces them, a decision table, every producer and consumer in each target, and a test matrix that could tell a right answer from a wrong one. One writer at a time owns a backend, and different models review each other’s deliveries.

Start together

  • A
    Values and declarations

    Storage, produced values, conversions and defaults.

  • B
    Flow and evaluation

    Binding identity, branch and loop joins, evaluation order.

  • C
    Identity and writable places

    Aliases, sharing, and where an assignment really lands.

  • E
    Intrinsics and platform

    Number and string domains, helper signatures, imports.

  • F
    Evidence and diagnostics

    Decision records and source mapping, outside selection.

After A and B agree on their interfaces

  • D
    Control-flow results

    Branch values, returns and exits, built from A’s representations and B’s flow facts, so no target has to rewrite return text.

weeks of agent sessions

Coding agents translated the engine and fixed the compiler. I designed how their work was divided and checked.

Porting a 20,000-line Kotlin engine to Haxe and getting five generated languages to match it took weeks of sessions. Over that time a single agent loses decisions as its context fills, and it tends to idle once it has started a sub-agent.

I set the work up as a long-running project with separate roles, each in its own session. Select a role to see what it does; the dot follows one ticket.

in parallelone session per ticketarchitecture questionsstartsreview reportResearcherStudies the current stateTech LeaderArchitecture questionsProject ManagerTracks progressTicket sessionOne per ticketReviewerReviews the work

Tech Leader

Opens the set of tickets from the research. After that it only answers architecture questions from ticket sessions and splits a ticket that has gone out of control into smaller ones. It holds no schedule, so its context stays on the design.

  • Two coordinators in parallel

    Design and schedule live in separate sessions.

    The Tech Leader answers architecture questions and splits tickets that have gone out of control. The Project Manager tracks progress. Each context holds one kind of work: schedule updates stay out of the Tech Leader’s context, and design discussion stays out of the Project Manager’s.

  • One session per ticket

    Each ticket starts with a fresh context.

    A ticket names the symptom, the root cause with file and line, the fix, how to verify it and when it is done. The session that works on it starts from that ticket and the repository.

  • Review inside the ticket

    The ticket session starts its own reviewer.

    An agent that has started a sub-agent tends to stop and wait for it. So only ticket sessions start sub-agents, for review, and they keep working on the findings. The report goes to the Tech Leader as a message.

  • Machine checks decide done

    A ticket closes on command output.

    Commit hooks refuse new warnings or style hits, and refuse when a checker produces no count. Progress counts come from the command that measured them. Every generated target runs the engine’s tests and is compared with the Kotlin engine.

Every generated language runs the same 1,386 engine tests ported from Kotlin.

Compile errors in the generated code

September 8
6,546
September 10
4,394

Passing build configurations out of 12

October 11
5
October 16
12

A check counts only when it has been shown to fail on a wrong answer.

A passing report can rest on a check that never ran, so I wrote down what each layer of checking proves and when it misleads. A report has to name the layer its evidence comes from, the commit it ran on, and the items it skipped.

  1. L1

    Generation

    The compiler exits 0 and the expected files exist.

    An input that never reaches a crash also exits 0, with wrong output.

  2. L2

    Types

    The target’s type checker reports no errors.

    Swift’s type check misses a closure that lost its return, and a failed upstream unit hides every unit that depends on it.

  3. L3

    Build

    The generated code compiles and links.

    Diagnostics from the build phase never appear in a type check alone.

  4. L4

    Behavior

    Test output matches the reference byte for byte.

    A wrong reference makes everything wrong, and properties it never prints go unchecked.

  5. L5

    Discrimination

    The same check fails against a backend that is wrong on purpose.

    One 38-line emitter change left the whole generated tree byte-identical. Without this layer, green can mean the property was never observed.

from a version tag to five package managers

One version tag publishes the engine and its protocol to the package managers of five languages.

Apps install Tiqian the way their language expects: npm for TypeScript, cargo for Rust, Swift Package Manager for Swift, Pub for Dart and Gradle for Kotlin. Each wants its own manifest, archive and registry.

The manifests around generated code used to be written by hand, and they broke whenever the generator renamed a file or moved the runtime. I moved packaging into the compiler and designed the release around it.

  1. boring.json

    One project file

    Thirteen target configurations: the engine and the wire protocol for each language, with a package name, version and license each.

  2. boring pack

    The compiler packs what it wrote

    The compilation that emits the code also writes its manifest and install file, from its own list of written files.

  3. GitHub release

    Upload on the version tag

    Every artifact is attached to the release. Swift archives go to object storage, because release assets cannot be served as zip.

  4. registry repository

    Commit metadata after the upload

    One metadata file per package and platform is committed only after publishing succeeded, so a failed release never appears.

  5. Cloudflare Pages

    Generate one static registry

    The registry tool reads the committed files and writes every ecosystem’s index. Downloads redirect to the release assets.

The packer knows each ecosystem’s format, and the registry speaks each one’s protocol.

Languageboring pack writesThe registry servesThe app configures
TypeScript.tgz with compiled JavaScriptnpm packumentsnpm --registry
Rust.cratecargo sparse index.cargo/config.toml
Swift.zipSwift package registryswift package-registry set
Dart.tar.gzPub hosted repositoryPUB_HOSTED_URL
Kotlinjar, pom and checksumsMaven metadataGradle maven { url }

I compared three ways to serve the packages and chose a generated static site.

Run five registry servers

Verdaccio for npm, a sparse-index server for cargo, and one server each for Swift, Pub and Maven. Five services to operate and five stores to keep in step with one list of releases.

Install from release links

Every app points at release download URLs by hand. Version choice and “latest” would live in each app’s own notes, wired again for every consumer.

Generate one static site

A single scan of the committed metadata writes all five indexes. The same metadata always produces the same site, and every document can be read and diffed.

This is the one I built.

The site holds no artifact bytes and no server code, and it has no upload endpoint. The registry tool is written in Haxe and compiled by boring itself, so the release path runs on the same compiler it ships.

looking back

What I would change, and what I would keep.

My work on Tiqian runs when the window moves, when a site builds, and when a new platform needs the engine. Each part had to make the page faster without changing a single line break.

  • Duo123 created Tiqian, researched CLREQ and wrote the Kotlin engine and its typography rules, from June 2026
  • Browser runtime: I rebuilt the scheduler around one coordinator and a measured frame budget, from August
  • Build path: I wrote the Rust addon, the font session and the layout cache, with the equivalence audits
  • boring and the port: I designed the compiler, its specifications and the agent project that wrote and fixed the code, about 3,400 commits

What I would change

  • Porting to Rust before measuring the cost of two copiesThe port was fast and correct, and within a week it had created most of the duplication boring later removed.
  • 32-bit floats for geometryThey cause the sub-pixel differences between targets. A move to 64-bit has been evaluated and is still open.
  • Layout still runs on the main threadWhen a host page runs long tasks, typesetting waits. A worker would need its own decision record first.

What I would defend

  • One owner for every frameThe flicker, the early off-screen commits and the stranded paragraphs all came from having two.
  • Equivalence audits before speed claimsA faster build that moves one line is a regression a reader can see.
  • Kotlin as the baseline for every targetEvery generated language is compared with the engine readers use today.