Skip to main content

hpl_cjk_text/
ffi.rs

1//! C ABI for the 32-bit game exe. Header: `include/hpl_cjk_text.h` (cbindgen, checked by
2//! `tests/header.rs`). Rules: fixed-width types only, no panic crosses the boundary, Rust
3//! never keeps caller pointers.
4
5use crate::{break_allowed, decode_utf8_bmp, is_cjk};
6use std::panic::{catch_unwind, AssertUnwindSafe};
7
8/// Bumped whenever an exported signature or struct changes.
9pub const HPL_CJK_ABI_VERSION: u32 = 1;
10
11/// Success.
12pub const HPL_CJK_OK: i32 = 0;
13/// `dst_cap` was too small; `out.len` holds the length needed.
14pub const HPL_CJK_BUFFER_TOO_SMALL: i32 = 1;
15/// A required pointer was null.
16pub const HPL_CJK_NULL_POINTER: i32 = -1;
17/// Internal error (a caught panic).
18pub const HPL_CJK_PANIC: i32 = -2;
19
20/// Result of [`hpl_cjk_decode_utf8`].
21#[repr(C)]
22#[derive(Clone, Copy, Debug, Default)]
23pub struct HplCjkDecodeResult {
24    /// UTF-16 code units written (or needed, with `HPL_CJK_BUFFER_TOO_SMALL`).
25    pub len: u32,
26    /// Malformed UTF-8 sequences replaced with U+FFFD.
27    pub invalid: u32,
28    /// Code points above U+FFFF replaced with U+FFFD.
29    pub astral: u32,
30}
31
32#[no_mangle]
33pub extern "C" fn hpl_cjk_abi_version() -> u32 {
34    HPL_CJK_ABI_VERSION
35}
36
37/// Decodes UTF-8 into BMP-only UTF-16. `dst_cap >= src_len` always suffices.
38///
39/// # Safety
40/// `src` must point to `src_len` readable bytes (may be null when `src_len` is 0), `dst` to
41/// `dst_cap` writable u16s (may be null when `dst_cap` is 0), and `out` to a writable result.
42#[no_mangle]
43pub unsafe extern "C" fn hpl_cjk_decode_utf8(
44    src: *const u8,
45    src_len: u32,
46    dst: *mut u16,
47    dst_cap: u32,
48    out: *mut HplCjkDecodeResult,
49) -> i32 {
50    if out.is_null() || (src.is_null() && src_len > 0) || (dst.is_null() && dst_cap > 0) {
51        return HPL_CJK_NULL_POINTER;
52    }
53    let bytes: &[u8] = if src_len == 0 { &[] } else { std::slice::from_raw_parts(src, src_len as usize) };
54    let decoded = catch_unwind(AssertUnwindSafe(|| decode_utf8_bmp(bytes)));
55    let Ok((units, stats)) = decoded else {
56        return HPL_CJK_PANIC;
57    };
58    let result = HplCjkDecodeResult { len: units.len() as u32, invalid: stats.invalid, astral: stats.astral };
59    *out = result;
60    if units.len() > dst_cap as usize {
61        return HPL_CJK_BUFFER_TOO_SMALL;
62    }
63    if !units.is_empty() {
64        std::ptr::copy_nonoverlapping(units.as_ptr(), dst, units.len());
65    }
66    HPL_CJK_OK
67}
68
69fn to_char(unit: u32) -> Option<char> {
70    char::from_u32(unit)
71}
72
73/// 1 if a line may break between UTF-16 code units `prev` and `next`, else 0.
74#[no_mangle]
75pub extern "C" fn hpl_cjk_break_allowed(prev: u32, next: u32) -> u32 {
76    match (to_char(prev), to_char(next)) {
77        (Some(a), Some(b)) => catch_unwind(|| break_allowed(a, b)).unwrap_or(false) as u32,
78        _ => 0,
79    }
80}
81
82/// 1 if `c` counts as CJK for line breaking, else 0.
83#[no_mangle]
84pub extern "C" fn hpl_cjk_is_cjk(c: u32) -> u32 {
85    to_char(c).map(|ch| catch_unwind(|| is_cjk(ch)).unwrap_or(false)).unwrap_or(false) as u32
86}