use crate::parser::hwp3::johab_map;
pub fn decode_johab_araea_jamo(ch: u16) -> Option<(Option<char>, char, Option<char>)> {
if ch < 0x8000 {
return None;
}
let cho_idx = ((ch >> 10) & 0x1F) as usize;
let jung_idx = (ch >> 5) & 0x1F;
let jong_idx = (ch & 0x1F) as usize;
if jung_idx != 30 {
return None;
}
let cho_map: [i32; 32] = [
-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
];
let jong_map: [i32; 32] = [
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, -1, -1,
];
let cho = *cho_map.get(cho_idx)?;
let jong = *jong_map.get(jong_idx)?;
if jong < 0 {
return None;
}
const CHO_FILL_INDEX: usize = 1;
if cho < 0 && cho_idx != CHO_FILL_INDEX {
return None;
}
let leading = if cho < 0 {
None
} else {
Some(char::from_u32(0x1100 + cho as u32)?)
};
let araea = char::from_u32(0x119E)?;
let trailing = if jong == 0 {
None
} else {
char::from_u32(0x11A7 + jong as u32)
};
Some((leading, araea, trailing))
}
pub fn decode_johab(ch: u16) -> char {
if ch < 0x80 {
return ch as u8 as char;
} else if ch >= 0x8000 {
let cho_idx = (ch >> 10) & 0x1F;
let jung_idx = (ch >> 5) & 0x1F;
let jong_idx = ch & 0x1F;
let cho_map: [i32; 32] = [
-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
];
let jung_map: [i32; 32] = [
-1, -1, -1, 0, 1, 2, 3, 4, -1, -1, 5, 6, 7, 8, 9, 10, -1, -1, 11, 12, 13, 14, 15, 16,
-1, -1, 17, 18, 19, 20, -1, -1,
];
let jong_map: [i32; 32] = [
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, -1, -1,
];
let cho = cho_map[cho_idx as usize];
let jung = jung_map[jung_idx as usize];
let jong = jong_map[jong_idx as usize];
if cho != -1 && jung != -1 && jong != -1 {
let uni_val = 0xAC00 + (cho * 21 * 28) + (jung * 28) + jong;
if let Some(c) = std::char::from_u32(uni_val as u32) {
return c;
}
}
if let Ok(idx) = johab_map::JOHAB_SYMBOLS.binary_search_by_key(&ch, |&(k, _)| k) {
return johab_map::JOHAB_SYMBOLS[idx].1;
}
} else if ch >= 0x0080 {
if let Some(c) = decode_hwp3_extra(ch) {
return c;
}
}
'?'
}
fn ksc5601_char(row: u8, cell: u8) -> Option<char> {
if !(0xA1..=0xFE).contains(&row) || !(0xA1..=0xFE).contains(&cell) {
return None;
}
let bytes = [row, cell];
let (text, _, had_errors) = encoding_rs::EUC_KR.decode(&bytes);
if had_errors {
return None;
}
let mut it = text.chars();
let c = it.next()?;
if it.next().is_some() {
return None;
}
Some(hancom_variant(c))
}
fn decode_hwp3_ksc_symbol(ch: u16) -> Option<char> {
const BASE: u16 = 0x3401;
const ROW_STRIDE: u16 = 96;
if ch < BASE {
return None;
}
let idx = ch - BASE;
let row = 0xA1u16.checked_add(idx / ROW_STRIDE)?;
let cell = 0xA1u16.checked_add(idx % ROW_STRIDE)?;
if row > 0xAC {
return None;
}
ksc5601_char(u8::try_from(row).ok()?, u8::try_from(cell).ok()?)
}
fn decode_hwp3_ksc_hanja(ch: u16) -> Option<char> {
const BASE: u16 = 0x4000;
const ROW_STRIDE: u16 = 94;
if ch < BASE {
return None;
}
let idx = ch - BASE;
let row = 0xCAu16.checked_add(idx / ROW_STRIDE)?;
let cell = 0xA1u16.checked_add(idx % ROW_STRIDE)?;
if row > 0xFD {
return None;
}
ksc5601_char(u8::try_from(row).ok()?, u8::try_from(cell).ok()?)
}
fn decode_hwp3_extra(ch: u16) -> Option<char> {
if (0x00A0..=0x00FF).contains(&ch) {
return char::from_u32(ch as u32);
}
if (0x3590..=0x3599).contains(&ch) {
return char::from_u32(0x2160 + (ch - 0x3590) as u32);
}
if (0x36E7..=0x36F0).contains(&ch) {
return char::from_u32(0x2460 + (ch - 0x36E7) as u32);
}
if (0x37C0..=0x37C5).contains(&ch) {
return char::from_u32(0xF03EF + (ch - 0x37C0) as u32);
}
let codepoint: u32 = match ch {
0x0081 => 0x201C, 0x0082 => 0x201D, 0x0083 => 0x2018, 0x0084 => 0x2019, 0x301E => 0xF0811, 0x301C => 0xF080F, 0x3024 => 0xF0817, 0x3027 => 0xF081A, 0x3404 => 0x2024, 0x3446 => 0x2192, 0x35E1 => 0x2500, 0x303D => 0xF0827, 0x3479 => 0x25B7, 0x347A => 0x25B6, 0x3441 => 0x25A0, 0x2F67 => 0x25B8, 0x2F00 => 0x25A1, 0x3366 => 0xF03C5,
0x203B => 0x203B,
0x2015 => 0x2015,
0x2010 => 0x2010, 0x2013 => 0x2013, 0x2103 => 0x2103, 0x2113 => 0x2113, 0x2190 => 0x2190, 0x2192 => 0x2192, 0x2193 => 0x2193, 0x2219 => 0x2219, 0x22C5 => 0x22C5, 0x2024 => 0x30FB, 0x2058 => 0x25B3, 0x205A => 0x25CB, 0x2F08 => 0x25AA, 0x2F11 => 0x25E6, 0x2F14 => 0x25E6, 0x3157 => 0x2027, 0x0480 => 0x02D0, 0x1F2E => 0x306E, 0x32B0 => 0xFF70, 0x3067 => 0x2018, 0x3068 => 0x2019, 0x309B => 0xFF62, 0x309D => 0xFF63, 0x3013 => 0xF0806,
0x3014 => 0xF0807,
0x3015 => 0xF0808,
0x3019 => 0xF080C,
0x301B => 0xF080E,
0x301D => 0xF0810,
0x37ED => 0x2550,
0x2E01 => 0x2460,
0x2E02 => 0x2461,
0x2E03 => 0x2462,
0x2E04 => 0x2463,
0x2E05 => 0x2464,
0x2E06 => 0x2465,
0x2E07 => 0x2466,
0x2E00 => 0xF0288,
0x2E0A => 0xF0289,
0x2E0B => 0xF028A,
0x2E0D => 0xF028C,
0x2E0E => 0xF028D,
0x2E0F => 0xF028E,
0x2E10 => 0xF028F,
0x2E11 => 0xF0290,
0x2E12 => 0xF0291,
0x2C21 => 0x24D0, 0x2C22 => 0x24D1,
0x2C23 => 0x24D2,
0x2C24 => 0x24D3,
0x2C25 => 0x24D4,
0x2C26 => 0x24D5,
0x2C40 => 0x3260, 0x2C41 => 0x3261,
0x2C42 => 0x3262,
0x2022 => 0x2022, 0x2F17 => 0x2022, 0x2F06 => 0x25A0, 0x25F5 => 0xF0099, 0x2BCE => 0xF012B,
0x3048 => 0xF0832, _ => return decode_hwp3_ksc_symbol(ch).or_else(|| decode_hwp3_ksc_hanja(ch)),
};
char::from_u32(codepoint)
}
fn hancom_variant(c: char) -> char {
match c {
'\u{223C}' => '\u{FF5E}',
'\u{2299}' => '\u{25C9}',
other => other,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_johab_rejects_reserved_jong_index() {
let jong_idx = 0;
let ch: u16 = 0x8000 | (2 << 10) | (3 << 5) | jong_idx;
assert_ne!(decode_johab(ch), '가');
}
#[test]
fn decode_johab_araea_preserves_legacy_jamo_sequence() {
assert_eq!(
decode_johab_araea_jamo(0xD3C5),
Some((Some('ᄒ'), 'ᆞ', Some('ᆫ')))
);
}
#[test]
fn decode_hwp3_company_graphics_to_the_common_hancom_pua() {
let decoded: String = (0x37C0..=0x37C5).map(decode_johab).collect();
assert_eq!(
decoded,
"\u{F03EF}\u{F03F0}\u{F03F1}\u{F03F2}\u{F03F3}\u{F03F4}"
);
}
#[test]
fn decode_hwp3_table_triangle_bullet() {
assert_eq!(decode_johab(0x2F67), '▸');
}
#[test]
fn ksc_hanja_rule_decodes_real_hwp3_codes() {
assert_eq!(decode_johab(0x4F5D), '債');
assert_eq!(decode_johab(0x4222), '權');
}
#[test]
fn ksc_symbol_rule_reproduces_hardcoded_mappings() {
assert_eq!(decode_johab(0x3446), '→');
assert_eq!(decode_johab(0x3441), '■');
assert_eq!(decode_johab(0x3479), '▷');
assert_eq!(decode_johab(0x347A), '▶');
assert_eq!(decode_johab(0x35E1), '─');
let roman: String = (0x3590..=0x3599).map(decode_johab).collect();
assert_eq!(roman, "ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ");
let circled: String = (0x36E7..=0x36F0).map(decode_johab).collect();
assert_eq!(circled, "①②③④⑤⑥⑦⑧⑨⑩");
}
#[test]
fn ksc_symbol_rule_recovers_lost_symbols() {
assert_eq!(decode_johab(0x3438), '※');
assert_eq!(decode_johab(0x343B), '○');
assert_eq!(decode_johab(0x3440), '□');
assert_eq!(decode_johab(0x2F00), '□');
assert_eq!(decode_johab(0x341C), '【');
assert_eq!(decode_johab(0x341D), '】');
}
#[test]
fn hardcoded_mapping_wins_over_rule() {
assert_eq!(decode_johab(0x37C1), '\u{F03F0}');
assert_eq!(decode_johab(0x3366), '\u{F03C5}');
}
#[test]
fn only_measured_private_codes_are_mapped() {
assert_eq!(decode_johab(0x203B), '※');
assert_eq!(decode_johab(0x205A), '○');
assert_eq!(decode_johab(0x2024), '・');
assert_eq!(decode_johab(0x2058), '△');
for ch in [0x0085_u16, 0x2059, 0x2F09] {
assert_eq!(
decode_johab(ch),
'?',
"0x{ch:04X} 는 근거 없이 매핑하면 안 된다"
);
}
}
#[test]
fn hancom_tilde_variant_matches_hangul() {
assert_eq!(hancom_variant('\u{223C}'), '\u{FF5E}');
assert_eq!(ksc5601_char(0xA1, 0xAD), Some('\u{FF5E}'));
}
}