aboutsummaryrefslogtreecommitdiff
path: root/Lib/fontTools/unicodedata/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/fontTools/unicodedata/__init__.py')
-rw-r--r--Lib/fontTools/unicodedata/__init__.py154
1 files changed, 81 insertions, 73 deletions
diff --git a/Lib/fontTools/unicodedata/__init__.py b/Lib/fontTools/unicodedata/__init__.py
index 4546ef3f..808c9c72 100644
--- a/Lib/fontTools/unicodedata/__init__.py
+++ b/Lib/fontTools/unicodedata/__init__.py
@@ -1,7 +1,11 @@
+from __future__ import annotations
+
from fontTools.misc.textTools import byteord, tostr
import re
from bisect import bisect_right
+from typing import Literal, TypeVar, overload
+
try:
# use unicodedata backport compatible with python2:
@@ -14,7 +18,7 @@ except ImportError: # pragma: no cover
from . import Blocks, Scripts, ScriptExtensions, OTTags
-__all__ = [tostr(s) for s in (
+__all__ = [
# names from built-in unicodedata module
"lookup",
"name",
@@ -39,11 +43,11 @@ __all__ = [tostr(s) for s in (
"script_horizontal_direction",
"ot_tags_from_script",
"ot_tag_to_script",
-)]
+]
def script(char):
- """ Return the four-letter script code assigned to the Unicode character
+ """Return the four-letter script code assigned to the Unicode character
'char' as string.
>>> script("a")
@@ -64,11 +68,11 @@ def script(char):
# contains the given codepoint: i.e. whose start is less than or equal
# to the codepoint. Thus, we subtract -1 from the index returned.
i = bisect_right(Scripts.RANGES, code)
- return Scripts.VALUES[i-1]
+ return Scripts.VALUES[i - 1]
def script_extension(char):
- """ Return the script extension property assigned to the Unicode character
+ """Return the script extension property assigned to the Unicode character
'char' as a set of string.
>>> script_extension("a") == {'Latn'}
@@ -80,7 +84,7 @@ def script_extension(char):
"""
code = byteord(char)
i = bisect_right(ScriptExtensions.RANGES, code)
- value = ScriptExtensions.VALUES[i-1]
+ value = ScriptExtensions.VALUES[i - 1]
if value is None:
# code points not explicitly listed for Script Extensions
# have as their value the corresponding Script property value
@@ -89,7 +93,7 @@ def script_extension(char):
def script_name(code, default=KeyError):
- """ Return the long, human-readable script name given a four-letter
+ """Return the long, human-readable script name given a four-letter
Unicode script code.
If no matching name is found, a KeyError is raised by default.
@@ -113,8 +117,7 @@ def _normalize_property_name(string):
return _normalize_re.sub("", string).lower()
-_SCRIPT_CODES = {_normalize_property_name(v): k
- for k, v in Scripts.NAMES.items()}
+_SCRIPT_CODES = {_normalize_property_name(v): k for k, v in Scripts.NAMES.items()}
def script_code(script_name, default=KeyError):
@@ -140,86 +143,90 @@ def script_code(script_name, default=KeyError):
# https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o
RTL_SCRIPTS = {
# Unicode-1.1 additions
- 'Arab', # Arabic
- 'Hebr', # Hebrew
-
+ "Arab", # Arabic
+ "Hebr", # Hebrew
# Unicode-3.0 additions
- 'Syrc', # Syriac
- 'Thaa', # Thaana
-
+ "Syrc", # Syriac
+ "Thaa", # Thaana
# Unicode-4.0 additions
- 'Cprt', # Cypriot
-
+ "Cprt", # Cypriot
# Unicode-4.1 additions
- 'Khar', # Kharoshthi
-
+ "Khar", # Kharoshthi
# Unicode-5.0 additions
- 'Phnx', # Phoenician
- 'Nkoo', # Nko
-
+ "Phnx", # Phoenician
+ "Nkoo", # Nko
# Unicode-5.1 additions
- 'Lydi', # Lydian
-
+ "Lydi", # Lydian
# Unicode-5.2 additions
- 'Avst', # Avestan
- 'Armi', # Imperial Aramaic
- 'Phli', # Inscriptional Pahlavi
- 'Prti', # Inscriptional Parthian
- 'Sarb', # Old South Arabian
- 'Orkh', # Old Turkic
- 'Samr', # Samaritan
-
+ "Avst", # Avestan
+ "Armi", # Imperial Aramaic
+ "Phli", # Inscriptional Pahlavi
+ "Prti", # Inscriptional Parthian
+ "Sarb", # Old South Arabian
+ "Orkh", # Old Turkic
+ "Samr", # Samaritan
# Unicode-6.0 additions
- 'Mand', # Mandaic
-
+ "Mand", # Mandaic
# Unicode-6.1 additions
- 'Merc', # Meroitic Cursive
- 'Mero', # Meroitic Hieroglyphs
-
+ "Merc", # Meroitic Cursive
+ "Mero", # Meroitic Hieroglyphs
# Unicode-7.0 additions
- 'Mani', # Manichaean
- 'Mend', # Mende Kikakui
- 'Nbat', # Nabataean
- 'Narb', # Old North Arabian
- 'Palm', # Palmyrene
- 'Phlp', # Psalter Pahlavi
-
+ "Mani", # Manichaean
+ "Mend", # Mende Kikakui
+ "Nbat", # Nabataean
+ "Narb", # Old North Arabian
+ "Palm", # Palmyrene
+ "Phlp", # Psalter Pahlavi
# Unicode-8.0 additions
- 'Hatr', # Hatran
- 'Hung', # Old Hungarian
-
+ "Hatr", # Hatran
+ "Hung", # Old Hungarian
# Unicode-9.0 additions
- 'Adlm', # Adlam
-
+ "Adlm", # Adlam
# Unicode-11.0 additions
- 'Rohg', # Hanifi Rohingya
- 'Sogo', # Old Sogdian
- 'Sogd', # Sogdian
-
+ "Rohg", # Hanifi Rohingya
+ "Sogo", # Old Sogdian
+ "Sogd", # Sogdian
# Unicode-12.0 additions
- 'Elym', # Elymaic
-
+ "Elym", # Elymaic
# Unicode-13.0 additions
- 'Chrs', # Chorasmian
- 'Yezi', # Yezidi
-
+ "Chrs", # Chorasmian
+ "Yezi", # Yezidi
# Unicode-14.0 additions
- 'Ougr', # Old Uyghur
+ "Ougr", # Old Uyghur
}
-def script_horizontal_direction(script_code, default=KeyError):
- """ Return "RTL" for scripts that contain right-to-left characters
+
+HorizDirection = Literal["RTL", "LTR"]
+T = TypeVar("T")
+
+
+@overload
+def script_horizontal_direction(script_code: str, default: T) -> HorizDirection | T:
+ ...
+
+
+@overload
+def script_horizontal_direction(
+ script_code: str, default: type[KeyError] = KeyError
+) -> HorizDirection:
+ ...
+
+
+def script_horizontal_direction(
+ script_code: str, default: T | type[KeyError] = KeyError
+) -> HorizDirection | T:
+ """Return "RTL" for scripts that contain right-to-left characters
according to the Bidi_Class property. Otherwise return "LTR".
"""
if script_code not in Scripts.NAMES:
if isinstance(default, type) and issubclass(default, KeyError):
raise default(script_code)
return default
- return str("RTL") if script_code in RTL_SCRIPTS else str("LTR")
+ return "RTL" if script_code in RTL_SCRIPTS else "LTR"
def block(char):
- """ Return the block property assigned to the Unicode character 'char'
+ """Return the block property assigned to the Unicode character 'char'
as a string.
>>> block("a")
@@ -231,23 +238,21 @@ def block(char):
"""
code = byteord(char)
i = bisect_right(Blocks.RANGES, code)
- return Blocks.VALUES[i-1]
+ return Blocks.VALUES[i - 1]
def ot_tags_from_script(script_code):
- """ Return a list of OpenType script tags associated with a given
+ """Return a list of OpenType script tags associated with a given
Unicode script code.
Return ['DFLT'] script tag for invalid/unknown script codes.
"""
+ if script_code in OTTags.SCRIPT_EXCEPTIONS:
+ return [OTTags.SCRIPT_EXCEPTIONS[script_code]]
+
if script_code not in Scripts.NAMES:
return [OTTags.DEFAULT_SCRIPT]
- script_tags = [
- OTTags.SCRIPT_EXCEPTIONS.get(
- script_code,
- script_code[0].lower() + script_code[1:]
- )
- ]
+ script_tags = [script_code[0].lower() + script_code[1:]]
if script_code in OTTags.NEW_SCRIPT_TAGS:
script_tags.extend(OTTags.NEW_SCRIPT_TAGS[script_code])
script_tags.reverse() # last in, first out
@@ -256,7 +261,7 @@ def ot_tags_from_script(script_code):
def ot_tag_to_script(tag):
- """ Return the Unicode script code for the given OpenType script tag, or
+ """Return the Unicode script code for the given OpenType script tag, or
None for "DFLT" tag or if there is no Unicode script associated with it.
Raises ValueError if the tag is invalid.
"""
@@ -278,6 +283,9 @@ def ot_tag_to_script(tag):
if tag in OTTags.NEW_SCRIPT_TAGS_REVERSED:
return OTTags.NEW_SCRIPT_TAGS_REVERSED[tag]
+ if tag in OTTags.SCRIPT_EXCEPTIONS_REVERSED:
+ return OTTags.SCRIPT_EXCEPTIONS_REVERSED[tag]
+
# This side of the conversion is fully algorithmic
# Any spaces at the end of the tag are replaced by repeating the last
@@ -285,7 +293,7 @@ def ot_tag_to_script(tag):
# Change first char to uppercase
script_code = tag[0].upper() + tag[1]
for i in range(2, 4):
- script_code += (script_code[i-1] if tag[i] == " " else tag[i])
+ script_code += script_code[i - 1] if tag[i] == " " else tag[i]
if script_code not in Scripts.NAMES:
return None