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