__init__.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2022 The Google Fonts Tools Authors.
  4. # Copyright 2017,2022 Google LLC All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS-IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. """
  19. Helper API for interaction with languages/regions/scripts
  20. data on the Google Fonts collection.
  21. """
  22. import glob
  23. import os
  24. import unicodedata
  25. from gflanguages import languages_public_pb2
  26. from google.protobuf import text_format
  27. from pkg_resources import resource_filename
  28. try:
  29. from ._version import version as __version__ # type: ignore
  30. except ImportError:
  31. __version__ = "0.0.0+unknown"
  32. DATA_DIR = resource_filename("gflanguages", "data")
  33. def LoadLanguages(base_dir=DATA_DIR):
  34. if base_dir is None:
  35. base_dir = DATA_DIR
  36. languages_dir = os.path.join(base_dir, "languages")
  37. langs = {}
  38. for textproto_file in glob.iglob(os.path.join(languages_dir, "*.textproto")):
  39. with open(textproto_file, "r", encoding="utf-8") as f:
  40. language = text_format.Parse(f.read(), languages_public_pb2.LanguageProto())
  41. langs[language.id] = language
  42. return langs
  43. def LoadScripts(base_dir=DATA_DIR):
  44. if base_dir is None:
  45. base_dir = DATA_DIR
  46. scripts_dir = os.path.join(base_dir, "scripts")
  47. scripts = {}
  48. for textproto_file in glob.iglob(os.path.join(scripts_dir, "*.textproto")):
  49. with open(textproto_file, "r", encoding="utf-8") as f:
  50. script = text_format.Parse(f.read(), languages_public_pb2.ScriptProto())
  51. scripts[script.id] = script
  52. return scripts
  53. def LoadRegions(base_dir=DATA_DIR):
  54. if base_dir is None:
  55. base_dir = DATA_DIR
  56. regions_dir = os.path.join(base_dir, "regions")
  57. regions = {}
  58. for textproto_file in glob.iglob(os.path.join(regions_dir, "*.textproto")):
  59. with open(textproto_file, "r", encoding="utf-8") as f:
  60. region = text_format.Parse(f.read(), languages_public_pb2.RegionProto())
  61. regions[region.id] = region
  62. return regions
  63. def parse(exemplars: str):
  64. """Parses a list of exemplar characters into a set of codepoints."""
  65. codepoints = set()
  66. for chars in exemplars.split():
  67. if len(chars) > 1:
  68. chars = chars.lstrip("{").rstrip("}")
  69. normalized_chars = unicodedata.normalize("NFC", chars)
  70. if normalized_chars != chars:
  71. for char in normalized_chars:
  72. codepoints.add(char)
  73. for char in chars:
  74. codepoints.add(char)
  75. return codepoints