2015-06-16 22:28:35 +01:00
|
|
|
"""fontTools.misc.encodingTools.py -- tools for working with OpenType encodings.
|
2015-04-19 03:36:20 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import print_function, division, absolute_import
|
|
|
|
from fontTools.misc.py23 import *
|
|
|
|
import fontTools.encodings.codecs
|
|
|
|
|
|
|
|
# Map keyed by platformID, then platEncID, then possibly langID
|
2015-04-26 00:54:30 -04:00
|
|
|
_encodingMap = {
|
2015-04-19 03:36:20 -07:00
|
|
|
0: { # Unicode
|
2015-04-24 12:48:37 -07:00
|
|
|
0: 'utf_16_be',
|
|
|
|
1: 'utf_16_be',
|
|
|
|
2: 'utf_16_be',
|
|
|
|
3: 'utf_16_be',
|
|
|
|
4: 'utf_16_be',
|
|
|
|
5: 'utf_16_be',
|
|
|
|
6: 'utf_16_be',
|
2015-04-19 03:36:20 -07:00
|
|
|
},
|
|
|
|
1: { # Macintosh
|
|
|
|
# See
|
|
|
|
# https://github.com/behdad/fonttools/issues/236
|
|
|
|
0: { # Macintosh, platEncID==0, keyed by langID
|
2015-04-19 04:24:55 -07:00
|
|
|
15: "mac_iceland",
|
|
|
|
17: "mac_turkish",
|
2015-04-24 12:32:20 -07:00
|
|
|
18: "mac_croatian",
|
2015-04-19 04:24:55 -07:00
|
|
|
24: "mac_latin2",
|
|
|
|
25: "mac_latin2",
|
|
|
|
26: "mac_latin2",
|
|
|
|
27: "mac_latin2",
|
|
|
|
28: "mac_latin2",
|
|
|
|
36: "mac_latin2",
|
2015-04-24 12:32:20 -07:00
|
|
|
37: "mac_romanian",
|
2015-04-19 04:24:55 -07:00
|
|
|
38: "mac_latin2",
|
|
|
|
39: "mac_latin2",
|
|
|
|
40: "mac_latin2",
|
|
|
|
Ellipsis: 'mac_roman', # Other
|
2015-04-19 03:36:20 -07:00
|
|
|
},
|
2015-04-19 04:24:55 -07:00
|
|
|
1: 'x_mac_japanese_ttx',
|
2015-04-19 04:46:12 -07:00
|
|
|
2: 'x_mac_trad_chinese_ttx',
|
2015-04-19 04:24:55 -07:00
|
|
|
3: 'x_mac_korean_ttx',
|
|
|
|
6: 'mac_greek',
|
|
|
|
7: 'mac_cyrillic',
|
2015-04-19 04:46:12 -07:00
|
|
|
25: 'x_mac_simp_chinese_ttx',
|
2015-04-19 04:24:55 -07:00
|
|
|
29: 'mac_latin2',
|
|
|
|
35: 'mac_turkish',
|
|
|
|
37: 'mac_iceland',
|
2015-04-19 03:36:20 -07:00
|
|
|
},
|
|
|
|
2: { # ISO
|
|
|
|
0: 'ascii',
|
2015-04-24 12:48:37 -07:00
|
|
|
1: 'utf_16_be',
|
2015-04-19 03:36:20 -07:00
|
|
|
2: 'latin1',
|
|
|
|
},
|
|
|
|
3: { # Microsoft
|
2015-04-24 12:48:37 -07:00
|
|
|
0: 'utf_16_be',
|
|
|
|
1: 'utf_16_be',
|
2015-04-19 04:24:55 -07:00
|
|
|
2: 'shift_jis',
|
2015-04-19 03:36:20 -07:00
|
|
|
3: 'gb2312',
|
|
|
|
4: 'big5',
|
2015-04-19 04:36:52 -07:00
|
|
|
5: 'euc_kr',
|
2015-04-19 03:36:20 -07:00
|
|
|
6: 'johab',
|
2015-04-24 12:48:37 -07:00
|
|
|
10: 'utf_16_be',
|
2015-04-19 03:36:20 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def getEncoding(platformID, platEncID, langID, default=None):
|
|
|
|
"""Returns the Python encoding name for OpenType platformID/encodingID/langID
|
|
|
|
triplet. If encoding for these values is not known, by default None is
|
|
|
|
returned. That can be overriden by passing a value to the default argument.
|
|
|
|
"""
|
|
|
|
encoding = _encodingMap.get(platformID, {}).get(platEncID, default)
|
|
|
|
if isinstance(encoding, dict):
|
|
|
|
encoding = encoding.get(langID, encoding[Ellipsis])
|
|
|
|
return encoding
|