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

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

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

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.1.0 — engine complete for horizontal text, 958 tests green (144 corpus × width combinations (24 paragraphs × 6 measures) checked against six invariants, plus golden cases). Not yet on PyPI.

Next:

  • 0.2 font probing across Windows/macOS/Linux, pair compression (「。」 in one em), a Knuth–Plass-style cost function so the last line stops being an orphan
  • 0.3 adapters: ReportLab, SVG, python-pptx; vertical text behind a flag
  • 0.4 a cards example that renders real 1080×1440 image sets, and a SKILL.md wrapper so agents can call it

License

MIT. See LICENSE.

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

Metadata

Release files for ziban 0.1.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.1.0
File Size Uploaded Provenance Actions
ziban-0.1.0.tar.gz 84.9 kB Without provenance Details for ziban-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 108.0 kB

Release files / ziban-0.1.0.tar.gz

Download URL ziban-0.1.0.tar.gz
Size 84.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e4ee1074d0f85f68245e52fb34292071b1ff52bb93743874629d65d51ac27493
BLAKE2b-256 checksum
How to use checksums
5e0b11b91a267eff9dfcedc81bb0eb839bca04a92cb9bc42f158549f84d300a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

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

Download URL ziban-0.1.0-py3-none-any.whl
Size 23.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
711330e16a9484ea7475b488007e0afe2d253a90446352890b73ce6deded7746
BLAKE2b-256 checksum
How to use checksums
3104ba75ede87f5d15f9772e411890b5ab5ea9b9d303b2a33b30841801235712
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for ziban 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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