Skip to main content

ziban

Correct CJK line breaking for everything that is not a browser.

中文说明 →

Browsers got text-spacing-trim, line-break: strict and proper kinsoku years ago. Everything else — Pillow, ReportLab, SVG, ffmpeg subtitles, python-pptx, canvas — still reimplements it badly, because there is nothing to ask.

ziban is that thing: a pure-Python typesetting engine that turns a string and a box into lines a Chinese typesetter would accept — and tells you, in machine readable form, every time it had to compromise.

Same text, same column: naive chunking on the left, ziban on the right

The problem, in eight seconds

Naive if len(s) > N: cut():

他说:「这次不一样。
」然后关掉了对话框。

ziban:

他说:「这次不一样。」
然后关掉了对话框。

The first version starts a line with 」. That is not a matter of taste: 《中文排版需求》(CLREQ) forbids closing punctuation at line start (避头) and opening punctuation at line end (避尾). Every renderer that is not a browser quietly breaks that rule — and the punctuation it breaks is exactly the punctuation readers notice.

The image above is generated, not drawn by hand: python examples/compare.py --font <path-to-a-CJK-font>. Re-run it after any change to the rules and you will see immediately whether the typography got better or worse.

Install

PyPI ci license

pip install ziban              # core, zero dependencies
pip install "ziban[pillow]"    # + measuring with real font files

Published at https://pypi.org/project/ziban/.

Python 3.9+. No browser, no native build, no ICU, no model.

Use it

As a CLI

$ echo "这是一个比较长的段落,用来看断行在真实文本上的表现。" \
    | ziban wrap - --width-em 8 --ruler
      +----------------+
  1   | 这是一个比较长的 |   8.0em
  2   | 段落,用来看断行 |   8.0em
  3   | 在真实文本上的表 |   8.0em
  4   | 现。             |   2.0em

4 line(s), box 8.0em, no diagnostics

> in the gutter means the line ends with a hanging mark, < means a compressed one, ! means it overflowed. --json gives you the same thing as structured data, for scripts and agents.

As a library

from ziban import Style, layout, EmMetrics

page = layout(
    "他说:「这次不一样。」然后关掉了对话框。",
    width=10 * 32,                     # 10 characters wide
    style=Style(size=32, leading=1.7),
    metrics=EmMetrics(32),             # or PillowMetrics("NotoSerifSC.otf", 32)
)

for line in page.lines:
    print(line.text, line.width)

page.diagnostics
# [Diagnostic(code='ORPHAN_LINE', line=1, ...)]

Every line carries its own explanation:

line.adjustments   # (Adjustment(kind='hang', index=10, delta=32.0),)
line.slack         # pixels left over
line.overflow      # pixels past the box, 0 if it fits
line.justify_spacing

Four card layouts, three themes

Twelve cards: four layouts in three themes

python examples/cards/build.py     # renders all 12 + the sheet above
ziban card examples/cards/specs/cover.json --theme ink --out cover.png

A spec is plain JSON — kind is one of cover / list / steps / note:

{
  "kind": "cover",
  "kicker": "003 / MAGPIE",
  "title": "把所有 agent 的模型入口,收成一处",
  "subtitle": "不是又一个聊天框。",
  "meta": "2026-11-09"
}

Themes: ikb (paper + Klein blue), ink (near-black + orange), citron (acid yellow). If a block does not fit its slot the size steps down until it does, and only then does it report TEXT_OVERFLOW — a card is never silently clipped. Boundaries are checked, not eyeballed:

$ ziban card spec.json --theme ink
wrote spec.png (1080x1440, theme=ink, fonts=NotoSansSC-VF.ttf / …)

What it does

避头尾 (kinsoku) closing marks never start a line, opening marks never end one
标点挤压 (compression) a full-width mark is drawn at half width when that is what makes the line fit
行末悬挂 (hanging) a mark may sit outside the margin rather than be pushed down — capped at one em and a quarter of the measure
中西混排 breaks between CJK and Latin, never inside a Latin word, numbers stay glued to their units
硬断行 \n is always honoured, blank lines survive
诊断 OVERFLOW, NO_BREAK_OPPORTUNITY, KINSOKU_FALLBACK, ORPHAN_LINE — with a suggested fix
确定性 same text + same font + same size → byte-identical output, on every platform

What it does not do

  • No AI. No API keys. No "generate my copy".
  • No browser, no headless Chromium, no layout engine to install.
  • No GUI editor.
  • No auto-publishing to any platform.
  • No bundled fonts. Microsoft YaHei and PingFang are not redistributable; bring your own, or use Noto Sans SC / Noto Serif SC (OFL).

Being explicit about the last one is not pedantry: a library that ships a proprietary font is a library companies cannot use.

Rules are data, not if-statements

The whole policy lives in src/ziban/data/clreq.toml:

[kinsoku]
head_forbidden = " ,、。.;:?!)〕〉》」』】”’}…"
tail_forbidden = "(〔〈《「『【“‘{"

[compress]
compressed_ratio = 0.5
chars = ",、。.;:?!)〕〉》」』】”’…"

[hang]
max_ratio = 1.0
max_share = 0.25

Change the file to change the behaviour. A Japanese ruleset is a second TOML, not a second engine. This is also why contributing is cheap: most typographic bugs are one line of data.

How it compares

kinsoku compression hanging works outside a browser deterministic
heti via browser partial ✗ ✗ ✗
pangu.py ✗ ✗ ✗ ✓ ✓
budoux ✗ ✗ ✗ ✓ ✓
browser text-spacing-trim ✓ ✓ partial ✗ ✓
ziban ✓ ✓ ✓ ✓ ✓

budoux decides where a break is semantically acceptable; ziban decides whether the break is typographically legal. They compose.

Status

0.2.0 — published on PyPI. Breaker complete for horizontal text; a drawing layer and four card templates on top of it. 994 tests green (144 corpus × width combinations — 24 paragraphs × 6 measures — checked against six invariants, plus golden cases, rendering tests and a 100k-character stress test).

Next:

  • 0.3 font probing beyond the bundled table, pair compression (「。」 in one em), a Knuth–Plass-style cost function so the last line stops being an orphan
  • 0.4 adapters: ReportLab, SVG, python-pptx; vertical text behind a flag

License

MIT. See LICENSE.

The ruleset follows CLREQ (W3C). Character classes follow a curated subset of UAX #14.

Metadata

Release files for ziban 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ziban 0.2.0
File Size Uploaded Provenance Actions
ziban-0.2.0.tar.gz 109.0 kB With provenance Details for ziban-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ziban 0.2.0
File Interpreter ABI Platform Actions
ziban-0.2.0-py3-none-any.whl Python 3 none any Details for ziban-0.2.0-py3-none-any.whl

Total release size: 143.2 kB

Release files / ziban-0.2.0.tar.gz

Download URL ziban-0.2.0.tar.gz
Size 109.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f7179014be55895ddbdb0ff980bd2a21ad5136f241d771144ab239ce8343cb88
BLAKE2b-256 checksum
How to use checksums
62b0daddcf8011094bdd67391996fe3c06c7b6d05b321a0b1de948c5b7c87af8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / ziban-0.2.0-py3-none-any.whl

Download URL ziban-0.2.0-py3-none-any.whl
Size 34.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4cd85692f2fd9d71c6d9bef86be4c6d306ca06a4e3890cfb8bc471ec95fbfb65
BLAKE2b-256 checksum
How to use checksums
82c726280272f10bbcc08ee15ff09263afffcf885cbf5e971c4beaca41246b99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Security for ziban 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ziban-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page