Skip to main content

fontoxide_misc/
encoding_tools.rs

1//! Port of `fontTools.misc.encodingTools`: the text encoding of a platform /
2//! encoding / language ID triplet.
3
4/// The Python codec name for an OpenType platformID / platEncID / langID triplet,
5/// or `None` if it is not known (`getEncoding`).
6///
7/// The names are those `fontoxide_encodings::codecs` understands.
8///
9/// ```
10/// use fontoxide_misc::encoding_tools::get_encoding;
11/// assert_eq!(get_encoding(3, 1, 0x409), Some("utf_16_be"));
12/// assert_eq!(get_encoding(1, 0, 15), Some("mac_iceland"));
13/// assert_eq!(get_encoding(1, 0, 0), Some("mac_roman"));
14/// assert_eq!(get_encoding(3, 7, 0), None);
15/// ```
16pub fn get_encoding(platform_id: u16, plat_enc_id: u16, lang_id: u16) -> Option<&'static str> {
17	Some(match platform_id {
18		// Unicode
19		0 => match plat_enc_id {
20			0..=6 => "utf_16_be",
21			_ => return None,
22		},
23		// Macintosh; see https://github.com/fonttools/fonttools/issues/236
24		1 => match plat_enc_id {
25			0 => match lang_id {
26				15 => "mac_iceland",
27				17 => "mac_turkish",
28				18 => "mac_croatian",
29				24..=28 | 36 | 38..=40 => "mac_latin2",
30				37 => "mac_romanian",
31				_ => "mac_roman",
32			},
33			1 => "x_mac_japanese_ttx",
34			2 => "x_mac_trad_chinese_ttx",
35			3 => "x_mac_korean_ttx",
36			6 => "mac_greek",
37			7 => "mac_cyrillic",
38			25 => "x_mac_simp_chinese_ttx",
39			29 => "mac_latin2",
40			35 => "mac_turkish",
41			37 => "mac_iceland",
42			_ => return None,
43		},
44		// ISO
45		2 => match plat_enc_id {
46			0 => "ascii",
47			1 => "utf_16_be",
48			2 => "latin1",
49			_ => return None,
50		},
51		// Microsoft
52		3 => match plat_enc_id {
53			0 | 1 | 10 => "utf_16_be",
54			2 => "shift_jis",
55			3 => "gb2312",
56			4 => "big5",
57			5 => "euc_kr",
58			6 => "johab",
59			_ => return None,
60		},
61		_ => return None,
62	})
63}