diff options
Diffstat (limited to 'tools/layoutlib/rename_font/build_font.py')
| -rwxr-xr-x | tools/layoutlib/rename_font/build_font.py | 102 |
1 files changed, 68 insertions, 34 deletions
diff --git a/tools/layoutlib/rename_font/build_font.py b/tools/layoutlib/rename_font/build_font.py index ea3dccc..aea3241 100755 --- a/tools/layoutlib/rename_font/build_font.py +++ b/tools/layoutlib/rename_font/build_font.py @@ -15,10 +15,10 @@ # limitations under the License. """ -Rename the PS name of all fonts in the input directory and copy them to the +Rename the PS name of all fonts in the input directories and copy them to the output directory. -Usage: build_font.py /path/to/input_fonts/ /path/to/output_fonts/ +Usage: build_font.py /path/to/input_fonts1/ /path/to/input_fonts2/ /path/to/output_fonts/ """ @@ -30,50 +30,86 @@ import os from lxml import etree import shutil import glob +from multiprocessing import Pool + +# global variable +dest_dir = '/tmp' def main(argv): - if len(argv) != 2: - print "Usage: build_font.py /path/to/input_fonts/ /path/to/out/dir/" - sys.exit(1) - if not os.path.isdir(argv[0]): - print argv[0] + "is not a valid directory" - sys.exit(1) - if not os.path.isdir(argv[1]): - print argv[1] + "is not a valid directory" - sys.exit(1) + if len(argv) < 2: + sys.exit('Usage: build_font.py /path/to/input_fonts/ /path/to/out/dir/') + for directory in argv: + if not os.path.isdir(directory): + sys.exit(directory + ' is not a valid directory') + global dest_dir + dest_dir = argv[-1] + src_dirs = argv[:-1] cwd = os.getcwd() - os.chdir(argv[1]) + os.chdir(dest_dir) files = glob.glob('*') for filename in files: os.remove(filename) os.chdir(cwd) - for filename in os.listdir(argv[0]): - if not os.path.splitext(filename)[1].lower() == ".ttf": - shutil.copy(os.path.join(argv[0], filename), argv[1]) - continue - print os.path.join(argv[0], filename) - old_ttf_path = os.path.join(argv[0], filename) + input_fonts = list() + for src_dir in src_dirs: + for dirname, dirnames, filenames in os.walk(src_dir): + for filename in filenames: + input_path = os.path.join(dirname, filename) + extension = os.path.splitext(filename)[1].lower() + if (extension == '.ttf'): + input_fonts.append(input_path) + elif (extension == '.xml'): + shutil.copy(input_path, dest_dir) + if '.git' in dirnames: + # don't go into any .git directories. + dirnames.remove('.git') + # Create as many threads as the number of CPUs + pool = Pool(processes=None) + pool.map(convert_font, input_fonts) + + +class InvalidFontException(Exception): + pass + +def convert_font(input_path): + filename = os.path.basename(input_path) + print 'Converting font: ' + filename + # the path to the output file. The file name is the fontfilename.ttx + ttx_path = os.path.join(dest_dir, filename) + ttx_path = ttx_path[:-1] + 'x' + try: # run ttx to generate an xml file in the output folder which represents all # its info - ttx_args = ["-d", argv[1], old_ttf_path] + ttx_args = ['-q', '-d', dest_dir, input_path] ttx.main(ttx_args) - # the path to the output file. The file name is the fontfilename.ttx - ttx_path = os.path.join(argv[1], filename) - ttx_path = ttx_path[:-1] + "x" # now parse the xml file to change its PS name. tree = etree.parse(ttx_path) encoding = tree.docinfo.encoding root = tree.getroot() for name in root.iter('name'): [old_ps_name, version] = get_font_info(name) - new_ps_name = old_ps_name + version - update_name(name, new_ps_name) + if old_ps_name is not None and version is not None: + new_ps_name = old_ps_name + version + update_name(name, new_ps_name) tree.write(ttx_path, xml_declaration=True, encoding=encoding ) # generate the udpated font now. - ttx_args = ["-d", argv[1], ttx_path] + ttx_args = ['-q', '-d', dest_dir, ttx_path] ttx.main(ttx_args) - # delete the temp ttx file. + except InvalidFontException: + # In case of invalid fonts, we exit. + print filename + ' is not a valid font' + raise + except Exception as e: + print 'Error converting font: ' + filename + print e + # Some fonts are too big to be handled by the ttx library. + # Just copy paste them. + shutil.copy(input_path, dest_dir) + try: + # delete the temp ttx file is it exists. os.remove(ttx_path) + except OSError: + pass def get_font_info(tag): ps_name = None @@ -85,19 +121,17 @@ def get_font_info(tag): if namerecord.attrib['nameID'] == '6': if ps_name is not None: if not sanitize(namerecord.text) == ps_name: - sys.exit('found multiple possibilities of the font name') + raise InvalidFontException('found multiple possibilities of the font name') else: ps_name = sanitize(namerecord.text) # nameID=5 means the font version if namerecord.attrib['nameID'] == '5': if ps_version is not None: if not ps_version == get_version(namerecord.text): - sys.exit('found multiple possibilities of the font version') + raise InvalidFontException('found multiple possibilities of the font version') else: ps_version = get_version(namerecord.text) - if ps_name is not None and ps_version is not None: - return [ps_name, ps_version] - sys.exit('didn\'t find the font name or version') + return [ps_name, ps_version] def update_name(tag, name): @@ -110,11 +144,11 @@ def sanitize(string): return re.sub(r'[^\w-]+', '', string) def get_version(string): - # The string must begin with "Version n.nn " + # The string must begin with 'Version n.nn ' # to extract n.nn, we return the second entry in the split strings. string = string.strip() - if not string.startswith("Version "): - sys.exit('mal-formed font version') + if not string.startswith('Version '): + raise InvalidFontException('mal-formed font version') return sanitize(string.split()[1]) if __name__ == '__main__': |
