跳轉至

hpl_cjk_text

Rust文字函式庫的Python模組:與遊戲相同的斷行與解碼規則,供翻譯工具使用。以pixi run develop-py建置並安裝到pixi環境。

這個模組是編譯過的,所以本頁由它的型別存根crates/hpl-cjk-text-py/hpl_cjk_text.pyi產生;有一項測試確保存根與模組保持一致。以下API說明為英文。

import hpl_cjk_text as cjk

cjk.break_allowed("字", "。")   # False:句號前不換行
cjk.break_allowed("字", "字")   # True:兩個 CJK 字元之間可以換行
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.