1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- """
- Helper API for interaction with languages/regions/scripts
- data on the Google Fonts collection.
- """
- import glob
- import os
- import unicodedata
- from gflanguages import languages_public_pb2
- from google.protobuf import text_format
- from pkg_resources import resource_filename
- try:
- from ._version import version as __version__
- except ImportError:
- __version__ = "0.0.0+unknown"
- DATA_DIR = resource_filename("gflanguages", "data")
- def LoadLanguages(base_dir=DATA_DIR):
- if base_dir is None:
- base_dir = DATA_DIR
- languages_dir = os.path.join(base_dir, "languages")
- langs = {}
- for textproto_file in glob.iglob(os.path.join(languages_dir, "*.textproto")):
- with open(textproto_file, "r", encoding="utf-8") as f:
- language = text_format.Parse(f.read(), languages_public_pb2.LanguageProto())
- langs[language.id] = language
- return langs
- def LoadScripts(base_dir=DATA_DIR):
- if base_dir is None:
- base_dir = DATA_DIR
- scripts_dir = os.path.join(base_dir, "scripts")
- scripts = {}
- for textproto_file in glob.iglob(os.path.join(scripts_dir, "*.textproto")):
- with open(textproto_file, "r", encoding="utf-8") as f:
- script = text_format.Parse(f.read(), languages_public_pb2.ScriptProto())
- scripts[script.id] = script
- return scripts
- def LoadRegions(base_dir=DATA_DIR):
- if base_dir is None:
- base_dir = DATA_DIR
- regions_dir = os.path.join(base_dir, "regions")
- regions = {}
- for textproto_file in glob.iglob(os.path.join(regions_dir, "*.textproto")):
- with open(textproto_file, "r", encoding="utf-8") as f:
- region = text_format.Parse(f.read(), languages_public_pb2.RegionProto())
- regions[region.id] = region
- return regions
- def parse(exemplars: str):
- """Parses a list of exemplar characters into a set of codepoints."""
- codepoints = set()
- for chars in exemplars.split():
- if len(chars) > 1:
- chars = chars.lstrip("{").rstrip("}")
- normalized_chars = unicodedata.normalize("NFC", chars)
- if normalized_chars != chars:
- for char in normalized_chars:
- codepoints.add(char)
- for char in chars:
- codepoints.add(char)
- return codepoints
|