summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDeepanshu Gupta <deepanshu@google.com>2014-05-29 15:37:54 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2014-05-29 15:37:55 +0000
commitf251b0608900499104da834758b89930a13ab1cc (patch)
tree4f9b687745aacb23e84736712933a8e380a09b61 /tools
parent28b13b2332ca38869df2825789620e66c6062672 (diff)
parentd23417ab5d1c623977d11b499c7f3bf2d5fb83b4 (diff)
downloadframeworks_base-f251b0608900499104da834758b89930a13ab1cc.zip
frameworks_base-f251b0608900499104da834758b89930a13ab1cc.tar.gz
frameworks_base-f251b0608900499104da834758b89930a13ab1cc.tar.bz2
Merge "Parallel processing of the fonts. [DO NOT MERGE]" into lmp-preview-dev
Diffstat (limited to 'tools')
-rwxr-xr-xtools/layoutlib/rename_font/build_font.py97
1 files changed, 60 insertions, 37 deletions
diff --git a/tools/layoutlib/rename_font/build_font.py b/tools/layoutlib/rename_font/build_font.py
index fe9b8c6..7f01a13 100755
--- a/tools/layoutlib/rename_font/build_font.py
+++ b/tools/layoutlib/rename_font/build_font.py
@@ -30,13 +30,18 @@ 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:
sys.exit('Usage: build_font.py /path/to/input_fonts/ /path/to/out/dir/')
- for dir in argv:
- if not os.path.isdir(dir):
- sys.exit(dir + ' is not a valid directory')
+ 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()
@@ -45,6 +50,7 @@ def main(argv):
for filename in files:
os.remove(filename)
os.chdir(cwd)
+ input_fonts = list()
for src_dir in src_dirs:
for filename in os.listdir(src_dir):
if os.path.isdir(os.path.join(src_dir, filename)):
@@ -52,37 +58,54 @@ def main(argv):
if not os.path.splitext(filename)[1].lower() == '.ttf':
shutil.copy(os.path.join(src_dir, filename), dest_dir)
continue
- old_ttf_path = os.path.join(src_dir, 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', dest_dir, old_ttf_path]
- ttx.main(ttx_args)
- # 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)
- 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', dest_dir, ttx_path]
- ttx.main(ttx_args)
- except Exception:
- # Some fonts are too big to be handled by the ttx library.
- # Just copy paste them.
- shutil.copy(old_ttf_path, dest_dir)
- try:
- # delete the temp ttx file is it exists.
- os.remove(ttx_path)
- except OSError:
- pass
+ input_fonts.append(os.path.join(src_dir, filename))
+ # 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 = ['-q', '-d', dest_dir, input_path]
+ ttx.main(ttx_args)
+ # 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)
+ 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 = ['-q', '-d', dest_dir, ttx_path]
+ ttx.main(ttx_args)
+ 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
@@ -94,14 +117,14 @@ 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)
return [ps_name, ps_version]
@@ -121,7 +144,7 @@ def get_version(string):
# 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')
+ raise InvalidFontException('mal-formed font version')
return sanitize(string.split()[1])
if __name__ == '__main__':