Skip to content

hpl_cjk_text

The Python module of the Rust text library: the same line-breaking and decoding rules the game uses, for translation tooling. Build it into the pixi environment with pixi run develop-py.

The module is compiled, so this page is generated from its type stub, crates/hpl-cjk-text-py/hpl_cjk_text.pyi, which a test keeps in step with the module.

import hpl_cjk_text as cjk

cjk.break_allowed("字", "。")   # False: never break before a full stop
cjk.break_allowed("字", "字")   # True: between two CJK characters
text, invalid, astral = cjk.decode_utf8("中文".encode())

UTF-8 decoding and CJK line breaking (避頭尾), the rules the AMFP Enhanced game uses.

A Python module built from the Rust crate hpl-cjk-text (PyO3, crates/hpl-cjk-text-py), so translation tooling breaks and decodes text exactly as the game does.

pixi run develop-py      # build and install it into the pixi environment
pixi run test-py         # its tests, including parity with the TC tooling

This stub describes the compiled module for editors, type checkers and the documentation; tests/python/test_stub.py checks that both list the same names.

NO_LINE_START module-attribute

NO_LINE_START: str

Characters that must never start a line: closing brackets and punctuation (、。,」』)!? ...).

NO_LINE_END module-attribute

NO_LINE_END: str

Characters that must never end a line: opening brackets and quotes (「『(《〈【〔“‘).

DEFAULT_ICON_PAD module-attribute

DEFAULT_ICON_PAD: dict[str, str]

Padding inserted after each $But<Action> variable whose default gamepad binding draws a button icon, keyed by action name (LB / RB icons are two em wide).

ABI_VERSION module-attribute

ABI_VERSION: int

Version of the C interface the game links against (HPL_CJK_ABI_VERSION).

insert_break_spaces

insert_break_spaces(text: str, wide_space: str = '\u3000', breaks: bool = True, icon_pad: dict[str, str] | None = None) -> str

Insert a space wherever a line may break, for the original game's word wrap.

The original engine breaks lines only at U+0020, so text for it gets a space at every allowed break. Markup tokens ([br], [uNNNN], $But<Action>) are kept whole. A port of the TC tooling's function with the same signature and defaults.

Parameters:

Name Type Description Default
text str

The text of one entry.

required
wide_space str

The wide space used for padding (default U+3000, ideographic space).

'\u3000'
breaks bool

Insert break spaces; when False, only the icon padding is applied.

True
icon_pad dict[str, str] | None

Padding after each $But<Action>; default DEFAULT_ICON_PAD.

None

Returns:

Type Description
str

The text with spaces at the break opportunities.

break_allowed

break_allowed(prev: str, next: str) -> bool

May a line break between prev and next? The rule the Enhanced game wraps with.

Never before a no-line-start character or a space, never after a no-line-end character, always after a space, between two CJK characters, and nowhere else (not inside Latin words, nor where Latin and CJK touch without a space).

Parameters:

Name Type Description Default
prev str

The character before the possible break (one character).

required
next str

The character after it (one character).

required

Returns:

Type Description
bool

True when a line may end after prev.

is_cjk

is_cjk(c: str) -> bool

Is c a CJK character for line-breaking purposes?

Parameters:

Name Type Description Default
c str

One character.

required

Returns:

Type Description
bool

True for CJK ideographs, kana, CJK symbols and punctuation, full-width forms, and

bool

the no-line-start / no-line-end characters.

decode_utf8

decode_utf8(data: bytes) -> tuple[str, int, int]

Decode UTF-8 the way the game reads its text files.

The engine stores text as 16-bit units, so code points above U+FFFF are replaced with U+FFFD (never written as surrogate pairs), as are malformed sequences.

Parameters:

Name Type Description Default
data bytes

The raw bytes.

required

Returns:

Type Description
str

(text, invalid, astral): the decoded text, the number of malformed sequences, and

int

the number of characters above U+FFFF that were replaced.