🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.2 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2020-2021 by
  4. # David Turner, Robert Wilhelm, and Werner Lemberg.
  5. #
  6. # This file is part of the FreeType project, and may only be used, modified,
  7. # and distributed under the terms of the FreeType project license,
  8. # LICENSE.TXT. By continuing to use, modify, or distribute this file you
  9. # indicate that you have read the license and understand and accept it
  10. # fully.
  11. """Parse modules.cfg and dump its output either as ftmodule.h or a list of
  12. base extensions.
  13. """
  14. from __future__ import print_function
  15. import argparse
  16. import os
  17. import re
  18. import sys
  19. # Expected input:
  20. #
  21. # ...
  22. # FONT_MODULES += <name>
  23. # HINTING_MODULES += <name>
  24. # RASTER_MODULES += <name>
  25. # AUX_MODULES += <name>
  26. # BASE_EXTENSIONS += <name>
  27. # ...
  28. def parse_modules_cfg(input_file):
  29. lists = {
  30. "FONT_MODULES": [],
  31. "HINTING_MODULES": [],
  32. "RASTER_MODULES": [],
  33. "AUX_MODULES": [],
  34. "BASE_EXTENSIONS": [],
  35. }
  36. for line in input_file.splitlines():
  37. line = line.rstrip()
  38. # Ignore empty lines and those that start with a comment.
  39. if not line or line[0] == "#":
  40. continue
  41. items = line.split()
  42. assert len(items) == 3 and items[1] == "+=", (
  43. "Unexpected input line [%s]" % line
  44. )
  45. assert items[0] in lists, (
  46. "Unexpected configuration variable name " + items[0]
  47. )
  48. lists[items[0]].append(items[2])
  49. return lists
  50. def generate_ftmodule(lists):
  51. result = "/* This is a generated file. */\n"
  52. for driver in lists["FONT_MODULES"]:
  53. if driver == "sfnt": # Special case for the sfnt 'driver'.
  54. result += "FT_USE_MODULE( FT_Module_Class, sfnt_module_class )\n"
  55. continue
  56. name = {
  57. "truetype": "tt",
  58. "type1": "t1",
  59. "cid": "t1cid",
  60. "type42": "t42",
  61. "winfonts": "winfnt",
  62. }.get(driver, driver)
  63. result += (
  64. "FT_USE_MODULE( FT_Driver_ClassRec, %s_driver_class )\n" % name
  65. )
  66. for module in lists["HINTING_MODULES"]:
  67. result += (
  68. "FT_USE_MODULE( FT_Module_Class, %s_module_class )\n" % module
  69. )
  70. for module in lists["RASTER_MODULES"]:
  71. name = {
  72. "raster": "ft_raster1",
  73. "smooth": "ft_smooth",
  74. }.get(module)
  75. result += (
  76. "FT_USE_MODULE( FT_Renderer_Class, %s_renderer_class )\n" % name
  77. )
  78. for module in lists["AUX_MODULES"]:
  79. if module in ("psaux", "psnames", "otvalid", "gxvalid"):
  80. result += (
  81. "FT_USE_MODULE( FT_Module_Class, %s_module_class )\n" % module
  82. )
  83. result += "/* EOF */\n"
  84. return result
  85. def generate_main_modules(lists):
  86. return "\n".join(
  87. lists["FONT_MODULES"]
  88. + lists["HINTING_MODULES"]
  89. + lists["RASTER_MODULES"]
  90. )
  91. def generate_aux_modules(lists):
  92. return "\n".join(lists["AUX_MODULES"])
  93. def generate_base_extensions(lists):
  94. return "\n".join(lists["BASE_EXTENSIONS"])
  95. def main():
  96. parser = argparse.ArgumentParser(description=__doc__)
  97. parser.add_argument(
  98. "--format",
  99. required=True,
  100. choices=(
  101. "ftmodule.h",
  102. "main-modules",
  103. "aux-modules",
  104. "base-extensions-list",
  105. ),
  106. help="Select output format.",
  107. )
  108. parser.add_argument(
  109. "input",
  110. metavar="CONFIGURE_RAW",
  111. help="The input configure.raw file to parse.",
  112. )
  113. parser.add_argument("--output", help="Output file (default is stdout).")
  114. args = parser.parse_args()
  115. with open(args.input) as f:
  116. input_data = f.read()
  117. lists = parse_modules_cfg(input_data)
  118. if args.format == "ftmodule.h":
  119. result = generate_ftmodule(lists)
  120. elif args.format == "main-modules":
  121. result = generate_main_modules(lists)
  122. elif args.format == "aux-modules":
  123. result = generate_aux_modules(lists)
  124. elif args.format == "base-extensions-list":
  125. result = generate_base_extensions(lists)
  126. else:
  127. assert False, "Invalid output format!"
  128. if args.output:
  129. with open(args.output, "w") as f:
  130. f.write(result)
  131. else:
  132. print(result)
  133. return 0
  134. if __name__ == "__main__":
  135. sys.exit(main())